803 Bricks Falling When Hit

1. Question

We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.
We will do some erasures sequentially. Each time we want to do the erasure at the location (i, j), the brick (if it exists) on that location will disappear, and then some other bricks may drop because of that erasure.
Return an array representing the number of bricks that will drop after each erasure in sequence.
1
Example 1:
2
Input:
3
4
grid = [[1,0,0,0],[1,1,1,0]]
5
hits = [[1,0]]
6
7
Output:
8
[2]
9
10
Explanation:
11
12
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Copied!
1
Example 2:
2
Input:
3
4
grid = [[1,0,0,0],[1,1,0,0]]
5
hits = [[1,1],[1,0]]
6
7
Output:
8
[0,0]
9
10
Explanation:
11
12
When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping. Note that the erased brick (1, 0) will not be counted as a dropped brick.
Copied!
Note:
  • The number of rows and columns in the grid will be in the range [1, 200].
  • The number of erasures will not exceed the area of the grid.
  • It is guaranteed that each erasure will be different from any other erasure, and located inside the grid.
  • An erasure may refer to a location with no brick - if it does, no bricks drop.

2. Implementation

(1) Union Find
思路: 这题题设是当brick通过hits数组打碎后,所有和第一行没有连在一起的brick就会掉落,问有多少brick会掉落。步骤如下:
(1) 我们可以用union find做。但union find只能连接不能断开,所以我们应该先处理hit brick之后的grid,找到所有connected component. 对于与第一行相连的component。我们用m * n这个特殊id表示和top连接的component, 因为按照row * n + col的方式定义union find的node id,m * n这个id是grid所有cell无法转换,非常适合做一个特殊id
(2) 找到hit之后的所有connected components后,我们倒过来把hit之后的brick加回去,每加一次的时候,查看和top连接的component的size,如果比之前的大,则有falling bricks,个数为newSize - size - 1,这里newSize是加上hit之后,与connected component的size,size是之前与top连接的component的size, 这里还要再减1是因为我们不算被hit的brick
1
class Solution {
2
public int[] hitBricks(int[][] grid, int[][] hits) {
3
if (grid == null || grid.length == 0) {
4
return new int[0];
5
}
6
7
int m = grid.length;
8
int n = grid[0].length;
9
10
for (int[] hit : hits) {
11
if (grid[hit[0]][hit[1]] == 1) {
12
grid[hit[0]][hit[1]] = 2;
13
}
14
}
15
16
UnionFind uf = new UnionFind(m *n + 1);
17
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
18
19
// Get all connected components after the hit
20
for (int i = 0; i < m; i++) {
21
for (int j = 0; j < n; j++) {
22
if (grid[i][j] == 1) {
23
connectNeighbors(i, j, uf, directions, grid);
24
}
25
}
26
}
27
28
// Find the original size of the component connected to the top
29
int size = uf.size[uf.find(m * n)];
30
int[] res = new int[hits.length];
31
int newSize = 0;
32
33
// Reversely adding the hit brick to get size of connected component conencted to the top
34
// The number of falling bricks of each hit will be the difference fo the size of the component connected to top
35
for (int i = hits.length - 1; i >= 0; i--) {
36
int[] hit = hits[i];
37
if (grid[hit[0]][hit[1]] == 2) {
38
connectNeighbors(hit[0], hit[1], uf, directions, grid);
39
grid[hit[0]][hit[1]] = 1;
40
}
41
42
newSize = uf.size[uf.find(m * n)];
43
if (newSize > size) {
44
// we minus one here because the hit brick should not be counted
45
res[i] = newSize - size - 1;
46
}
47
size = newSize;
48
}
49
return res;
50
}
51
52
// Connect brick in 4 directions
53
public void connectNeighbors(int row, int col, UnionFind uf, int[][] directions, int[][] grid) {
54
int m = grid.length;
55
int n = grid[0].length;
56
57
for (int[] direction : directions) {
58
int nextRow = row + direction[0];
59
int nextCol = col + direction[1];
60
61
if (isValid(nextRow, nextCol, grid)) {
62
uf.union(row * n + col, nextRow * n + nextCol);
63
}
64
}
65
66
// Use m * n as the special node id for connected component connected to the top row
67
if (row == 0) {
68
uf.union(row * n + col, m * n);
69
}
70
}
71
72
public boolean isValid(int row, int col, int[][] grid) {
73
return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == 1;
74
}
75
76
class UnionFind {
77
int[] sets;
78
int[] size;
79
80
public UnionFind(int n) {
81
sets = new int[n];
82
size = new int[n];
83
84
for (int i = 0; i < n; i++) {
85
sets[i] = i;
86
size[i] = 1;
87
}
88
}
89
90
public int find(int node) {
91
while (node != sets[node]) {
92
node = sets[node];
93
}
94
return node;
95
}
96
97
public void union(int i, int j) {
98
int node1 = find(i);
99
int node2 = find(j);
100
101
if (node1 == node2) {
102
return;
103
}
104
105
sets[node1] = node2;
106
size[node2] += size[node1];
107
}
108
}
109
}
Copied!

3. Time & Space Complexity

Union Find: 时间复杂度O(mn * (k + mn)), k是hits数组的长度,unionfind里的find()最坏情况是O(mn), 在加上被hit的brick的过程中,我们每次都将被hit的brick的与四周union,这个过程要O(mn), 总共有k次,所以这步需要O(mnk). 另一部分时间是在刚开始找出在hit之后所有connected component的过程,这部分需要O(mn^2)。空间复杂度O(mn)