Prepin
Log in

Given an nxn grid consisting of 1s and 0s, return the total count of islands present in the input.

Approach / Explanation

This issue pertains to a classic algorithmic challenge in graph theory. An island is characterized as a collection of 1's that are connected in four directions (horizontally or vertically). Thus, when a 1 is identified in the grid, it is necessary to check if its neighboring cells (above, below, to the left, and to the right) also contain 1's. This cluster of 1's constitutes a single island, and the objective is to determine the total number of such islands in the specified nxn grid. To effectively tackle this problem, consider the following key points: Implement either a Depth First Search (DFS) or Breadth First Search (BFS) approach to explore the neighboring cells of each cell. Upon visiting a cell with a 1, all of its adjacent cells in the four directions, along with their unvisited neighboring cells containing 1's, should be recognized as part of the same island. Only count the unique islands.

Suggested Answer

In Python, you can utilize the Depth-First Search (DFS) method, which is particularly effective for problems like this one. Begin at a cell, and if it represents an island, perform DFS to label all neighboring islands, then increase the counter. Below is a straightforward implementation: def dfs ( grid , i , j ) : if i < 0 or j < 0 or i >= len ( grid ) or j >= len ( grid [ 0 ] ) or grid [ i ] [ j ] != '1' : return grid [ i ] [ j ] = '#' dfs ( grid , i + 1 , j ) dfs ( grid , i - 1 , j ) dfs ( grid , i , j + 1 ) dfs ( grid , i , j - 1 ) def numIslands ( grid ) : if not grid : return 0 count = 0 for i in range ( len ( grid ) ) : for j in range ( len ( grid [ 0 ] ) ) : if grid [ i ] [ j ] == '1' : dfs ( grid , i , j ) count += 1 return count In this code, the numIslands function receives the grid as input and returns the total number of islands. Within this function, for every unvisited cell containing a '1', we increase the count and invoke the dfs function to mark all cells in the current island as visited. The dfs function marks the current cell as visited and recursively calls itself for all neighboring cells.

Alternative Answer

Another method to address this issue is by employing a Breadth-First Search (BFS) technique: def numIslands ( grid ) : if not grid : return 0 count = 0 for i in range ( len ( grid ) ) : for j in range ( len ( grid [ 0 ] ) ) : if grid [ i ] [ j ] == '1' : bfs ( grid , i , j ) count += 1 return count def bfs ( grid , r , c ) : grid [ r ] [ c ] = '0' lst = [ ( r , c ) ] while lst : x , y = lst . pop ( 0 ) for r , c in ( ( x - 1 , y ) , ( x + 1 , y ) , ( x , y - 1 ) , ( x , y + 1 ) ) : if 0 <= r < len ( grid ) and 0 <= c < len ( grid [ 0 ] and grid [ r ] [ c ] == '1' : lst . append ( ( r , c ) ) grid [ r ] [ c ] = '0' In this code, the numIslands function accepts the grid as input and returns the total count of islands. Within the numIslands function, whenever an unvisited cell containing '1' is encountered, we increment the count and invoke the bfs function to mark all cells belonging to the current island as visited. The bfs function marks the current cell as visited and queues all its unvisited neighbors for processing.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.