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. For example:
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
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'
.# Number of Islands
This problem asks us to find the number of islands in a 2D grid, where '1' represents land and '0' represents water. An island is formed by connecting adjacent lands horizontally or vertically. We can solve this problem using Depth-First Search (DFS) or Breadth-First Search (BFS).
## Naive Solution (DFS)
The naive solution is to iterate through each cell in the grid. If we find a '1', we increment the island count and then perform a DFS to mark all adjacent land cells as visited (e.g., change them to '0'). This ensures that we don't count the same island multiple times.
```python
def num_islands_naive(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
island_count = 0
def dfs(row, col):
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == '0':
return
grid[row][col] = '0' # Mark as visited
dfs(row + 1, col)
dfs(row - 1, col)
dfs(row, col + 1)
dfs(row, col - 1)
for i in range(rows):
for j in range(cols):
if grid[i][j] == '1':
island_count += 1
dfs(i, j)
return island_count
The optimal solution is essentially the same as the naive one, but it's important to clearly articulate the algorithm and its efficiency.
def num_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
island_count = 0
def dfs(row, col):
# Check boundaries and if the cell is land
if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == '0':
return
# Mark the current cell as visited (water)
grid[row][col] = '0'
# Explore adjacent cells
dfs(row + 1, col) # Down
dfs(row - 1, col) # Up
dfs(row, col + 1) # Right
dfs(row, col - 1) # Left
for i in range(rows):
for j in range(cols):
if grid[i][j] == '1':
island_count += 1
dfs(i, j)
return island_count
The time complexity of this solution is O(M x N), where M is the number of rows and N is the number of columns in the grid.
The space complexity is O(M x N) in the worst-case scenario, where M is the number of rows and N is the number of columns in the grid.