Problem
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.
Example 1:
Input:
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
Output: [2]
Explanation:
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.
Example 2:
Input:
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
Output: [0,0]
Explanation:
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.
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.
reference: https://leetcode.com/problems...
Solution
class Solution {
public int[] hitBricks(int[][] grid, int[][] hits) {
int[] res = new int[hits.length];
int m = grid.length, n = grid[0].length;
//mark the to-hit bricks to 2
for (int[] hit: hits) {
int x = hit[0], y = hit[1];
if (grid[x][y] == 1) grid[x][y] = 2;
}
//m*n for grid elements, 1 for a virtual top "0"
UnionFind uf = new UnionFind(m*n+1);
//after bricks hitted, union the ones left
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) unionAround(grid, i, j, uf);
}
}
//the count of bricks left
int count = uf.sizes[uf.find(0)];
for (int i = hits.length-1; i >= 0; i--) {
int[] hit = hits[i];
int x = hit[0], y = hit[1];
if (grid[x][y] == 2) {
unionAround(grid, x, y, uf);
grid[x][y] = 1;
}
int newSize = uf.sizes[uf.find(0)];
res[i] = newSize-count > 0 ? newSize-count-1 : 0;
count = newSize;
}
return res;
}
private void unionAround(int[][] grid, int x, int y, UnionFind uf) {
int m = grid.length, n = grid[0].length;
int[] dx = new int[]{-1, 1, 0, 0};
int[] dy = new int[]{0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int nx = x+dx[i];
int ny = y+dy[i];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (grid[nx][ny] == 1) {
uf.union(x*n+y+1, nx*n+ny+1);
}
}
if (x == 0) uf.union(x*n+y+1, 0);
}
}
class UnionFind {
int[] parents;
int[] sizes;
public UnionFind(int n) {
parents = new int[n];
sizes = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i;
sizes[i] = 1;
}
}
public int find(int i) {
if (parents[i] != i) {
return find(parents[i]);
} else {
return i;
}
}
public void union(int a, int b) {
int pA = find(a);
int pB = find(b);
if (pA != pB) {
parents[pA] = pB;
sizes[pB] += sizes[pA];
}
}
}