MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / count_islands

Function count_islands

python3/Graphs/count_islands.py:4–15  ·  view source on GitHub ↗
(matrix: List[List[int]])

Source from the content-addressed store, hash-verified

2
3
4def count_islands(matrix: List[List[int]]) -> int:
5 if not matrix:
6 return 0
7 count = 0
8 for r in range(len(matrix)):
9 for c in range(len(matrix[0])):
10 # If a land cell is found, perform DFS to explore the full
11 # island, and include this island in our count.
12 if matrix[r][c] == 1:
13 dfs(r, c, matrix)
14 count += 1
15 return count
16
17def dfs(r: int, c: int, matrix: List[List[int]]) -> None:
18 # Mark the current land cell as visited.

Callers

nothing calls this directly

Calls 1

dfsFunction · 0.70

Tested by

no test coverage detected