-
Notifications
You must be signed in to change notification settings - Fork 17
/
number-of-islands.py
201 lines (164 loc) · 7.79 KB
/
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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import unittest
from collections import deque
from typing import List
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def find(array: List[int], pos: int) -> int:
root = pos
while root != array[root]:
root = array[root]
while pos != root:
array[pos], pos = root, array[pos]
return root
def union(root_left: int, root_right: int, array: List[int], count: List[int]):
root_less, root_more = sorted(
(root_left, root_right), key=lambda x: count[x]
)
array[root_less] = root_more
count[root_more] += count[root_less]
rows, cols = len(grid), len(grid[0]) if grid else 0
array = list(range(rows * cols))
count = [1] * (rows * cols)
islands = rows * cols
for row in range(rows):
for col in range(cols):
if grid[row][col] == "0":
islands -= 1
continue
for row_next, col_next in [
(row, col - 1),
(row - 1, col),
]:
if (
0 <= row_next < rows
and 0 <= col_next < cols
and grid[row_next][col_next] == "1"
):
root_left = find(array, row * cols + col)
root_right = find(array, row_next * cols + col_next)
if root_left != root_right:
union(root_left, root_right, array, count)
islands -= 1
return islands
def numIslandsDFSIter(self, grid: "List[List[str]]") -> "int":
visited = set()
islands_count = 0
for y in range(len(grid)):
for x in range(len(grid[0])):
if (x, y) not in visited and int(grid[y][x]):
islands_count += 1
stack = [(x, y)]
while len(stack):
point = stack.pop()
visited.add(point)
neighbours = [
(point[0] - 1, point[1]),
(point[0] + 1, point[1]),
(point[0], point[1] + 1),
(point[0], point[1] - 1),
]
for neigh in neighbours:
if (
neigh[0] >= len(grid[0])
or neigh[0] < 0
or neigh[1] >= len(grid)
or neigh[1] < 0
):
continue
if not int(grid[neigh[1]][neigh[0]]):
continue
if neigh not in visited:
stack.append(neigh)
return islands_count
def numIslandsBFS(self, grid):
queue = deque()
visited = {}
islands = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if (i, j) not in visited and int(grid[i][j]) == 1:
islands += 1
queue.append((i, j))
while len(queue):
m, n = queue.popleft()
if m < 0 or m >= len(grid) or n < 0 or n >= len(grid[0]):
continue
if (m, n) in visited:
continue
visited[(m, n)] = True
if int(grid[m][n]) == 0:
continue
neighbours = [
(m - 1, n),
(m + 1, n),
(m, n - 1),
(m, n + 1),
]
for neighbour in neighbours:
queue.append(neighbour)
return islands
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_empty(self):
self.assertEqual(self.sol.numIslands([]), 0)
def test_just_zero(self):
self.assertEqual(self.sol.numIslands([["0"]]), 0)
def test_just_one(self):
self.assertEqual(self.sol.numIslands([["1"]]), 1)
def test_1(self):
grid = [
["1", "1", "1", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"],
]
self.assertEqual(self.sol.numIslands(grid), 1)
def test_2(self):
grid = [
["1", "1", "0", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "0", "0", "0"],
]
self.assertEqual(self.sol.numIslands(grid), 2)
def test_3(self):
grid = [
["0", "1", "0", "1", "0"],
["1", "1", "0", "1", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "1"],
]
self.assertEqual(self.sol.numIslands(grid), 4)
def test_4(self):
grid = [
["1", "1", "0", "0", "0"],
["1", "1", "0", "0", "0"],
["0", "0", "1", "0", "0"],
["0", "0", "0", "1", "1"],
]
self.assertEqual(self.sol.numIslands(grid), 3)
def test_5(self):
grid = [
["1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "0", "1", "1", ],
["0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "0", ],
["1", "0", "1", "1", "1", "0", "0", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "0", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "0", "1", "1", "1", "0", "1", "1", "1", ],
["0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["0", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", ],
["1", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "0", "1", "1", "1", "1", "0", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "0", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "0", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", ],
]
self.assertEqual(self.sol.numIslands(grid), 1)