-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_305.java
More file actions
83 lines (71 loc) · 2.27 KB
/
Copy pathP_305.java
File metadata and controls
83 lines (71 loc) · 2.27 KB
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
package leetcode.hard;
import java.util.ArrayList;
import java.util.List;
public class P_305 {
private static final class UnionFind {
private final int[] parent;
private final int[] size;
private int count;
private UnionFind(int n) {
parent = new int[n];
size = new int[n];
}
public int find(int p) {
// path compression
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q) {
final int rootP = find(p);
final int rootQ = find(q);
if (rootP == rootQ) {
return;
}
// union by size
if (size[rootP] > size[rootQ]) {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
} else {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
count--;
}
public int count() { return count; }
public int[] size() { return size; }
public int getParent(int idx) {
return parent[idx];
}
public void add(int index) {
if (parent[index] != index) {
parent[index] = index;
size[index] = 1;
count++;
}
}
}
private static final int[][] DIRS = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
public List<Integer> numIslands2(int m, int n, int[][] positions) {
final UnionFind uf = new UnionFind(n * m + 1);
final List<Integer> res = new ArrayList<>();
for (int[] pos : positions) {
final int p = n * pos[0] + pos[1] + 1;
uf.add(p);
for (int[] dir : DIRS) {
final int nx = pos[0] + dir[0];
final int ny = pos[1] + dir[1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n) {
final int q = uf.getParent(n * nx + ny + 1);
if (q > 0 && uf.find(p) != uf.find(q)) {
uf.union(p, q);
}
}
}
res.add(uf.count());
}
return res;
}
}