305. Number of Islands II

305. Number of Islands II

Description

You are given an empty 2D binary grid grid of size m x n. The grid represents a map where 0‘s represent water and 1‘s represent land. Initially, all the cells of grid are water cells (i.e., all the cells are 0‘s).

We may perform an add land operation which turns the water at position into a land. You are given an array positions where positions[i] = [ri, ci] is the position (ri, ci) at which we should operate the i^th operation.

Return an array of integers answer where answer[i] is the number of islands after turning the cell (ri, ci) into a land.

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 1:

1
2
3
4
5
6
7
8
Input: m = 3, n = 3, positions = [[0,0],[0,1],[1,2],[2,1]]
Output: [1,1,2,3]
Explanation:
Initially, the 2d grid is filled with water.
- Operation #1: addLand(0, 0) turns the water at grid[0][0] into a land. We have 1 island.
- Operation #2: addLand(0, 1) turns the water at grid[0][1] into a land. We still have 1 island.
- Operation #3: addLand(1, 2) turns the water at grid[1][2] into a land. We have 2 islands.
- Operation #4: addLand(2, 1) turns the water at grid[2][1] into a land. We have 3 islands.

Example 2:

1
2
Input: m = 1, n = 1, positions = [[0,0]]
Output: [1]

Constraints:

  • 1 <= m, n, positions.length <= 10^4
  • 1 <= m * n <= 10^4
  • positions[i].length == 2
  • 0 <= ri < m
  • 0 <= ci < n

Follow up: Could you solve it in time complexity O(k log(mn)), where k == positions.length?

Hints/Notes

Solution

Language: C++

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
class Solution {
public:
// idea: union find
// 1. for each input point, check its adjacent points
// 2. if there's no adjacent component, then the new point itself is a new island
// if there's one commponent
static constexpr int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<int> numIslands2(int m, int n, vector<vector<int>>& positions) {
vector<int> parent(m * n, -1);
vector<int> res;
int numIsland = 0;
for (auto pos : positions) {
int x = pos[0], y = pos[1], root = -1;
if (parent[x * n + y] != -1) {
res.push_back(numIsland);
continue;
}
for (int k = 0; k < 4; k++) {
int dx = x + dirs[k][0], dy = y + dirs[k][1], encoding = dx * n + dy;
if (dx >= 0 && dx < m && dy >= 0 && dy < n && findRoot(encoding, parent) != -1) {
root = findRoot(encoding, parent);
break;
}
}
if (root == -1) {
numIsland++;
parent[x * n + y] = x * n + y;
} else {
int encoding = x * n + y;
merge(root, encoding, parent);
for (int k = 0; k < 4; k++) {
int dx = x + dirs[k][0], dy = y + dirs[k][1], encoding = dx * n + dy;
if (dx >= 0 && dx < m && dy >= 0 && dy < n) {
int curRoot = findRoot(encoding, parent);
if (curRoot != -1 && curRoot != root) {
merge(root, curRoot, parent);
numIsland--;
}
}
}
}
res.push_back(numIsland);
}
return res;
}

void merge(int root1, int root2, vector<int>& parent) {
parent[root2] = root1;
}

int findRoot(int enc, vector<int>& parent) {
if (parent[enc] == -1) {
return -1;
}
if (parent[enc] != enc) {
parent[enc] = findRoot(parent[enc], parent);
}
return parent[enc];
}
};