forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_islands_dfs.py
More file actions
55 lines (43 loc) · 1.25 KB
/
count_islands_dfs.py
File metadata and controls
55 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Count Islands (DFS)
Given a 2D grid of 1s (land) and 0s (water), count the number of islands
using depth-first search.
Reference: https://leetcode.com/problems/number-of-islands/
Complexity:
Time: O(M * N)
Space: O(M * N) recursion stack in worst case
"""
from __future__ import annotations
def num_islands(grid: list[list[int]]) -> int:
"""Return the number of islands in *grid*.
Args:
grid: 2D matrix of 0s and 1s (modified in place during traversal).
Returns:
Number of connected components of 1s.
Examples:
>>> num_islands([[1, 0], [0, 1]])
2
"""
count = 0
for i in range(len(grid)):
for j, col in enumerate(grid[i]):
if col == 1:
_dfs(grid, i, j)
count += 1
return count
def _dfs(grid: list[list[int]], i: int, j: int) -> None:
"""Flood-fill from (i, j), marking visited cells as 0.
Args:
grid: The grid (modified in place).
i: Row index.
j: Column index.
"""
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]):
return
if grid[i][j] != 1:
return
grid[i][j] = 0
_dfs(grid, i + 1, j)
_dfs(grid, i - 1, j)
_dfs(grid, i, j + 1)
_dfs(grid, i, j - 1)