forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution4.java
114 lines (91 loc) · 3.01 KB
/
Solution4.java
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
/// Source : https://leetcode.com/problems/number-of-islands/description/
/// Author : liuyubobobo
/// Time : 2018-08-26
import java.util.Arrays;
import java.util.HashSet;
/// Using Union-Find
/// Time Complexity: O(n*m)
/// Space Complexity: O(n*m)
class Solution4 {
private class UnionFind {
private int[] rank;
private int[] parent;
public UnionFind(int count){
rank = new int[count];
parent = new int[count];
for(int i = 0 ; i < count ; i ++){
parent[i] = i;
rank[i] = 1;
}
}
private int find(int p){
while(p != parent[p]){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public boolean isConnected(int p , int q){
return find(p) == find(q);
}
public void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if(pRoot == qRoot)
return;
if(rank[pRoot] < rank[qRoot])
parent[pRoot] = qRoot;
else if(rank[qRoot] < rank[pRoot])
parent[qRoot] = pRoot;
else{ // rank[pRoot] == rank[qRoot]
parent[pRoot] = qRoot;
rank[qRoot] += 1;
}
}
}
private int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
private int m, n;
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0)
return 0;
m = grid.length;
n = grid[0].length;
UnionFind uf = new UnionFind(m * n);
for(int i = 0 ; i < m ; i ++)
for(int j = 0; j < n ; j ++)
if(grid[i][j] == '1')
for(int k = 0; k < 4; k ++){
int newX = i + d[k][0], newY = j + d[k][1];
if(inArea(newX, newY) && grid[newX][newY] == '1')
uf.unionElements(i * n + j, newX * n + newY);
}
HashSet<Integer> islands = new HashSet<>();
for(int i = 0 ; i < m ; i ++)
for(int j = 0; j < n ; j ++)
if(grid[i][j] == '1')
islands.add(uf.find(i * n + j));
return islands.size();
}
private boolean inArea(int x, int y){
return x >= 0 && x < m && y >= 0 && y < n;
}
public static void main(String[] args) {
char grid1[][] = {
{'1','1','1','1','0'},
{'1','1','0','1','0'},
{'1','1','0','0','0'},
{'0','0','0','0','0'}
};
System.out.println((new Solution4()).numIslands(grid1));
// 1
// ---
char grid2[][] = {
{'1','1','0','0','0'},
{'1','1','0','0','0'},
{'0','0','1','0','0'},
{'0','0','0','1','1'}
};
System.out.println((new Solution4()).numIslands(grid2));
// 3
}
}