-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnumber-of-islands-ii.py
62 lines (49 loc) · 1.88 KB
/
number-of-islands-ii.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
from typing import List, Optional
class Solution:
def numIslands2(self, m: int, n: int, positions: List[List[int]]) -> List[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],
) -> None:
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]
FAKE_ROOT = -1
array = [FAKE_ROOT] * m * n
count = [1] * m * n
num_of_islands = 0
arr_pos = lambda r, c: r * n + c
result = []
for row, col in positions:
if array[arr_pos(row, col)] == FAKE_ROOT:
array[arr_pos(row, col)] = arr_pos(row, col)
num_of_islands += 1
for row_next, col_next in (
(row - 1, col),
(row + 1, col),
(row, col - 1),
(row, col + 1),
):
if (
0 <= row_next < m
and 0 <= col_next < n
and array[arr_pos(row_next, col_next)] != FAKE_ROOT
):
root_left = find(array, arr_pos(row, col))
root_right = find(array, arr_pos(row_next, col_next))
if root_left != root_right:
union(root_left, root_right, array, count)
num_of_islands -= 1
result.append(num_of_islands)
return result