-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_200.java
More file actions
110 lines (99 loc) · 3.15 KB
/
Copy pathP_200.java
File metadata and controls
110 lines (99 loc) · 3.15 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
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
package leetcode.medium;
public class P_200 {
private static final int[][] DIRS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
public int numIslandsDFS(char[][] grid) {
final int n = grid.length;
final int m = grid[0].length;
int res = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < m; col++) {
if (grid[row][col] == '1') {
res++;
dfs(row, col, grid);
}
}
}
return res;
}
private static void dfs(int row, int col, char[][] grid) {
if (row < 0 || row == grid.length || col < 0 || col == grid[0].length || grid[row][col] != '1') {
return;
}
grid[row][col] = '*';
for (int[] dir : DIRS) {
dfs(row + dir[0], col + dir[1], grid);
}
}
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];
count = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
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];
size[rootQ] = 0;
} else {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
size[rootP] = 0;
}
count--;
}
public int count() { return count; }
public int[] size() { return size; }
}
public int numIslands(char[][] grid) {
final int n = grid.length;
final int m = grid[0].length;
final UnionFind uf = new UnionFind(n * m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1') {
for (int[] dir : DIRS) {
final int ni = i + dir[0];
final int nj = j + dir[1];
final int u = i * m + j;
if (ni >= 0 && ni < n && nj >= 0 && nj < m && grid[ni][nj] == '1') {
final int v = ni * m + nj;
uf.union(u, v);
}
}
}
}
}
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
final int u = i * m + j;
if (grid[i][j] == '1' && uf.find(u) == u) {
res++;
}
}
}
return res;
}
}