305 Number of Islands II

1. Question

A 2d grid map ofmrows andncolumns is initially filled with water. We may perform anaddLandoperation which turns the water at position (row, col) into a land. Given a list of positions to operate,count the number of islands after eachaddLandoperation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example:
Givenm = 3, n = 3,positions = [[0,0], [0,1], [1,2], [2,1]]. Initially, the 2d gridgridis filled with water. (Assume 0 represents water and 1 represents land).
1
0 0 0
2
0 0 0
3
0 0 0
Copied!
Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land.
1
1 0 0
2
0 0 0 Number of islands = 1
3
0 0 0
Copied!
Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land.
1
1 1 0
2
0 0 0 Number of islands = 1
3
0 0 0
Copied!
Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land.
1
1 1 0
2
0 0 1 Number of islands = 2
3
0 0 0
Copied!
Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land.
1
1 1 0
2
0 0 1 Number of islands = 3
3
0 1 0
Copied!
We return the result as an array:[1, 1, 2, 3]
Challenge:
Can you do it in time complexity O(k log mn), where k is the length of thepositions?

2. Implementation

(1) Union Find
思路: 动态连接的话用Union Find最适合
1
class Solution {
2
public List<Integer> numIslands2(int m, int n, int[][] positions) {
3
List<Integer> res = new ArrayList();
4
5
if (m <= 0 || n <= 0) {
6
return res;
7
}
8
9
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
10
11
UnionFind uf = new UnionFind(m * n);
12
13
for (int[] position : positions) {
14
int row = position[0];
15
int col = position[1];
16
int id1 = row * n + col;
17
18
uf.add(id1);
19
20
for (int[] direction : directions) {
21
int nextRow = row + direction[0];
22
int nextCol = col + direction[1];
23
24
if (isValid(nextRow, nextCol, m, n)) {
25
int id2 = nextRow * n + nextCol;
26
27
if (uf.getSize(id2) >= 1) {
28
uf.union(id1, id2);
29
}
30
}
31
}
32
res.add(uf.count);
33
}
34
return res;
35
}
36
37
public boolean isValid(int row, int col, int m, int n) {
38
return row >= 0 && row < m && col >= 0 && col < n;
39
}
40
41
class UnionFind {
42
int[] sets;
43
int[] size;
44
int count;
45
46
public UnionFind(int n) {
47
sets = new int[n];
48
size = new int[n];
49
count = 0;
50
}
51
52
public void add(int id) {
53
sets[id] = id;
54
size[id] = 1;
55
++count;
56
}
57
58
public int getSize(int id) {
59
return size[id];
60
}
61
62
public int find(int node) {
63
while (node != sets[node]) {
64
node = sets[node];
65
}
66
return node;
67
}
68
69
public void union(int i, int j) {
70
int node1 = find(i);
71
int node2 = find(j);
72
73
if (node1 == node2) {
74
return;
75
}
76
77
if (size[node1] < size[node2]) {
78
sets[node1] = node2;
79
size[node2] += size[node1];
80
}
81
else {
82
sets[node2] = node1;
83
size[node1] += size[node2];
84
}
85
--count;
86
}
87
}
88
}
Copied!

3. Time & Space Complexity

时间复杂度O(k* log(mn)), 空间复杂度O(mn)