-
Notifications
You must be signed in to change notification settings - Fork 0
/
0200_number_of_islands.py
44 lines (38 loc) · 1.42 KB
/
0200_number_of_islands.py
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
import collections
from typing import List
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
num_rows = len(grid)
if num_rows == 0:
return 0
num_cols = len(grid[0])
visited = [[False] * num_cols for _ in range(num_rows)]
def reverse_max_region(row, col):
queue = collections.deque([(row, col)])
while queue:
x, y = queue.popleft()
if x < 0 or x >= num_rows or y < 0 or y >= num_cols or visited[x][y]:
continue
visited[x][y] = True
if grid[x][y] == '1':
grid[x][y] = '0'
top, down = x - 1, x + 1
left, right = y - 1, y + 1
queue.append((x, left))
queue.append((x, right))
queue.append((top, y))
queue.append((down, y))
num_island = 0
for row in range(num_rows):
for col in range(num_cols):
if grid[row][col] == '0':
continue
else:
reverse_max_region(row, col)
num_island += 1
return num_island
if __name__ == '__main__':
grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
expected = 1
ans = Solution().numIslands(grid)
print(ans == expected)