Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1
Example 2:
Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j] is '0' or '1'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The 'Number of Islands' problem asks us to count distinct landmasses surrounded by water on a map. The brute force strategy involves systematically exploring every piece of land and water on the map to identify and count these islands.
Here's how the algorithm would work step-by-step:
def number_of_islands_brute_force(grid): if not grid:
return 0
rows = len(grid)
columns = len(grid[0])
number_of_islands = 0
def explore_island(current_row, current_column):
# Ensure we are within bounds and on land ('1') that hasn't been visited
if (current_row < 0 or current_row >= rows or
current_column < 0 or current_column >= columns or
grid[current_row][current_column] == '0'):
return
# Mark the current land piece as visited by changing it to '0'
grid[current_row][current_column] = '0'
# Explore all adjacent land pieces recursively
explore_island(current_row + 1, current_column)
explore_island(current_row - 1, current_column)
explore_island(current_row, current_column + 1)
explore_island(current_row, current_column - 1)
# Iterate through each cell of the grid
for row_index in range(rows):
for column_index in range(columns):
# If we find an unvisited piece of land, it signifies the start of a new island
if grid[row_index][column_index] == '1':
number_of_islands += 1
explore_island(row_index, column_index)
return number_of_islandsThe core idea is to explore the map systematically, finding land and then 'sinking' it to avoid recounting. We'll use a process that's like dipping a paintbrush into the water and marking all connected land pieces we find.
Here's how the algorithm would work step-by-step:
def number_of_islands(grid_map):
if not grid_map:
return 0
rows_count = len(grid_map)
columns_count = len(grid_map[0])
number_of_islands_found = 0
def sink_connected_land(row_index, column_index):
# Base cases to stop recursion: out of bounds or already water
if (row_index < 0 or row_index >= rows_count or
column_index < 0 or column_index >= columns_count or
grid_map[row_index][column_index] == '0'):
return
# Mark the current land piece as visited by changing it to water.
grid_map[row_index][column_index] = '0'
# Recursively explore adjacent land cells (up, down, left, right).
sink_connected_land(row_index + 1, column_index)
sink_connected_land(row_index - 1, column_index)
sink_connected_land(row_index, column_index + 1)
sink_connected_land(row_index, column_index - 1)
# Iterate through each cell of the grid.
for current_row_index in range(rows_count):
for current_column_index in range(columns_count):
# If we find a land cell ('1'), it signifies the start of a new island.
if grid_map[current_row_index][current_column_index] == '1':
number_of_islands_found += 1
# Sink all connected land parts of this newly found island.
sink_connected_land(current_row_index, current_column_index)
return number_of_islands_found| Case | How to Handle |
|---|---|
| Empty grid or null grid input | The solution should return 0 islands if the grid is null or has zero rows/columns. |
| Grid with only water ('0's) | The algorithm will traverse the grid but find no '1's, correctly resulting in 0 islands. |
| Grid with only land ('1's) | The algorithm should identify and count this as a single large island. |
| A single row or single column grid | The logic should correctly identify islands that span the entire single dimension. |
| Grid with dimensions 1x1 | A single '1' should be counted as one island, and a single '0' as zero islands. |
| Islands touching the boundary of the grid | The problem statement guarantees edges are surrounded by water, so this case is implicitly handled. |
| A very large grid potentially causing stack overflow with recursive DFS | An iterative DFS or BFS approach using a stack or queue respectively would mitigate this risk. |
| Grid contains characters other than '0' or '1' | The solution should ideally handle or gracefully reject such invalid inputs, or assume valid input as per problem constraints. |