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.length
n == grid[i].length
1 <= m, n <= 300
grid[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:
We can think of the problem as finding connected groups of land on a map. The brute force method simply looks at every single piece of the map and, if it's land, tries to identify the entire island it belongs to.
Here's how the algorithm would work step-by-step:
def number_of_islands_brute_force(grid):
if not grid:
return 0
number_of_rows = len(grid)
number_of_columns = len(grid[0])
number_of_islands = 0
def explore_island(row, column):
# Check boundaries and if it's land
if row < 0 or row >= number_of_rows or \
column < 0 or column >= number_of_columns or \
grid[row][column] == '0':
return
# Mark the current land as visited
grid[row][column] = '0'
# Recursively explore all adjacent land
explore_island(row + 1, column)
explore_island(row - 1, column)
explore_island(row, column + 1)
explore_island(row, column - 1)
# Iterate through each cell in the grid
for row in range(number_of_rows):
for column in range(number_of_columns):
# If we find land, start exploring the island
if grid[row][column] == '1':
# Increment island count
number_of_islands += 1
# Explore the entire island
explore_island(row, column)
return number_of_islands
We need to find the number of separate landmasses in a map represented by ones and zeros. The key is to explore each landmass only once and mark it as visited, so we don't count it again. We can achieve this using a technique that spreads out from a starting point to find all connected land.
Here's how the algorithm would work step-by-step:
def number_of_islands(grid):
if not grid:
return 0
number_of_rows = len(grid)
number_of_columns = len(grid[0])
island_count = 0
def explore_island(row, column):
# Check boundaries and if the cell is land.
if row < 0 or row >= number_of_rows or \
column < 0 or column >= number_of_columns or \
grid[row][column] == '0':
return
# Mark the current cell as visited.
grid[row] = grid[row][:column] + '0' + grid[row][column+1:]
# Recursively explore adjacent land.
explore_island(row + 1, column)
explore_island(row - 1, column)
explore_island(row, column + 1)
explore_island(row, column - 1)
for row in range(number_of_rows):
for column in range(number_of_columns):
# Start island count when land is found
if grid[row][column] == '1':
island_count += 1
# Explore the entire island and mark it visited.
explore_island(row, column)
return island_count
Case | How to Handle |
---|---|
Null or empty grid | Return 0 if the grid is null or has zero rows or columns, as there can be no islands. |
Grid with all water ('0') | The algorithm should correctly identify that there are zero islands and return 0. |
Grid with all land ('1') | The algorithm should correctly identify that there is one island and return 1. |
Grid with a single '1' surrounded by '0's | The algorithm should correctly identify this as one island. |
Large grid with many islands | The algorithm's time and space complexity should be considered to ensure it scales efficiently without exceeding memory limits. |
Grid where islands are connected diagonally | The problem specifies horizontal and vertical connections only, so the algorithm must not consider diagonal connections. |
Grid with islands touching the grid boundaries | The algorithm should not treat boundaries differently; islands touching edges are still valid islands. |
Integer overflow when calculating grid dimensions | Use appropriate data types (e.g., long) if the grid dimensions could potentially lead to integer overflow. |