-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (49 loc) · 1.47 KB
/
main.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
class Solution:
def numIslands2(self, m: int, n: int, positions: List[List[int]]) -> List[int]:
mn = m * n
group = [i for i in range(mn)]
rank = [0] * mn
land = [False] * mn
def find(k):
nonlocal group
if group[k] == k:
return k
group[k] = find(group[k])
return group[k]
def unite(a, b):
nonlocal rank
ga = find(a)
gb = find(b)
if ga == gb:
return False
if rank[ga] < rank[gb]:
group[ga] = gb
elif rank[gb] < rank[ga]:
group[gb] = ga
else:
group[gb] = ga
rank[ga] += 1
return True
def rc2i(r, c):
return r * n + c
directions = ((1, 0), (-1, 0), (0, 1), (0, -1))
result = []
num = 0
for r, c in positions:
i = rc2i(r, c)
if land[i]:
result.append(num)
continue
land[i] = True
num += 1
for dr, dc in directions:
rr, cc = r + dr, c + dc
if rr < 0 or m <= rr or cc < 0 or n <= cc:
continue
j = rc2i(r + dr, c + dc)
if not land[j]:
continue
if unite(i, j):
num -= 1
result.append(num)
return result