-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200.numIslands.py
106 lines (93 loc) · 3.01 KB
/
200.numIslands.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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'.
#
from typing import List
from collections import deque
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
# mark visited cells
visited = [[False for _ in range(cols)] for _ in range(rows)]
# initialze the expansion for islands queue
queue = deque([])
# count number of islands
n_islands = 0
def bfs(r, c):
visited[r][c] = True
queue.append((r, c))
while queue:
row, col = queue.popleft() # NOTE: if use pop() will be dfs
# all the direction to move
directions = [
(0, 1), # try explore right
(0, -1), # try explore left
(1, 0), # try explore down
(-1, 0), # try explore up
]
for dr, dc in directions:
# check the new position is island and not visited
# while updated row and col is within the grid
if (
0 <= row + dr < rows
and 0 <= col + dc < cols
and grid[row + dr][col + dc] == "1"
and not visited[row + dr][col + dc]
):
# mark visited and add to queue
visited[row + dr][col + dc] = True
queue.append((row + dr, col + dc))
# iterate over the grid, see if can form island when take each cell as starting point
for r in range(rows):
for c in range(cols):
# start point has to be island and not visited
if grid[r][c] == "1" and not visited[r][c]:
bfs(r, c)
n_islands += 1
return n_islands
if __name__ == "__main__":
grid = [
["1", "1", "1", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"],
]
print(Solution().numIslands(grid)) # 1
grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"],
]
print(Solution().numIslands(grid)) # 3