-
Notifications
You must be signed in to change notification settings - Fork 820
/
BricksFallingWhenHit.java
227 lines (216 loc) · 6.87 KB
/
BricksFallingWhenHit.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* (C) 2024 YourCompanyName */
package depth_first_search;
import java.util.ArrayList;
import java.util.List;
/**
* Created by gouthamvidyapradhan on 01/07/2018. 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.
*
* <p>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.
*
* <p>Return an array representing the number of bricks that will drop after each erasure in
* sequence.
*
* <p>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.
*
* <p>Note:
*
* <p>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.
*
* <p>Solution: O(R x C): Erase all the bricks in the grid and do a union of all the bricks using a
* union-find disjoint set. (A modified union-find disjoint set is necessary to keep track of size
* of the connected component and to check if its connected to roof or not) Once you have the
* different connected components of the grid, solve the problem in the reverse order by iterating
* the hits in the reverse order. First set 1 in the grid for each hits and count the connected
* bricks in all four directions which are not linked to roof of the grid.
*/
public class BricksFallingWhenHit {
private static final int[] R = {0, 0, 1, -1};
private static final int[] C = {1, -1, 0, 0};
/** @author gouthamvidyapradhan Class to represent UnionFind Disjoint Set */
private static class UnionFind {
private int[] p;
private int[] rank;
private boolean[] roof;
private int[] size;
UnionFind(int s) {
this.p = new int[s];
this.rank = new int[s];
this.size = new int[s];
this.roof = new boolean[s];
init();
}
/** Initialize with its same index as its parent */
private void init() {
for (int i = 0; i < p.length; i++) {
p[i] = i;
size[i] = 1;
}
}
/**
* Find the representative vertex
*
* @param i
* @return
*/
private int findSet(int i) {
if (p[i] != i) {
p[i] = findSet(p[i]);
}
return p[i];
}
/**
* Set as roof
*
* @param i
*/
public void setAsRoof(int i) {
roof[i] = true;
}
/**
* Perform union of two vertex
*
* @param i
* @param j
* @return true if union is performed successfully, false otherwise
*/
public boolean union(int i, int j) {
int x = findSet(i);
int y = findSet(j);
if (x != y) {
if (rank[x] > rank[y]) {
p[y] = p[x];
roof[x] = (roof[x] || roof[y]);
size[x] = size[x] + size[y];
} else {
p[x] = p[y];
roof[y] = (roof[x] || roof[y]);
size[y] = size[x] + size[y];
if (rank[x] == rank[y]) {
rank[y]++; // increment the rank
}
}
return true;
}
return false;
}
/**
* is attached to roof
*
* @param i
* @return
*/
public boolean isRoof(int i) {
return roof[findSet(i)];
}
/**
* is attached to roof
*
* @param i
* @return
*/
public int size(int i) {
return size[findSet(i)];
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int[][] grid = {{1, 1, 1, 1, 1}, {0, 0, 1, 0, 1}, {1, 0, 1, 0, 1}, {1, 1, 1, 0, 1}};
int[][] hits = {{1, 2}, {2, 2}, {2, 4}, {0, 4}, {0, 0}};
int[] r = new BricksFallingWhenHit().hitBricks(grid, hits);
for (int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
}
public int[] hitBricks(int[][] grid, int[][] hits) {
int nR = grid.length;
int nC = grid[0].length;
UnionFind unionFind = new UnionFind((nR * nC) + 1);
for (int i = 0; i < nC; i++) {
if (grid[0][i] == 1) {
unionFind.setAsRoof(i + 1);
}
}
for (int k = 0; k < hits.length; k++) {
int[] h = hits[k];
if (grid[h[0]][h[1]] == 0) {
h[0] = -1;
h[1] = -1;
} else {
grid[h[0]][h[1]] = 0;
}
}
boolean[][] done = new boolean[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1 && !done[i][j]) {
dfs(i, j, grid, done, unionFind);
}
}
}
int[] result = new int[hits.length];
for (int i = hits.length - 1; i >= 0; i--) {
int[] h = hits[i];
int r = h[0];
int c = h[1];
if (r == -1) continue;
grid[r][c] = 1;
int cell = (r * nC) + c + 1;
int sum = 0;
List<Integer> notLinkedToRoof = new ArrayList<>();
List<Integer> linkedToRoof = new ArrayList<>();
for (int k = 0; k < 4; k++) {
int newR = r + R[k];
int newC = c + C[k];
if (newR >= 0 && newR < nR && newC >= 0 && newC < nC && grid[newR][newC] == 1) {
int newCell = (newR * nC) + newC + 1;
if (unionFind.isRoof(newCell)) {
linkedToRoof.add(newCell);
} else {
notLinkedToRoof.add(newCell);
}
}
}
for (int nr : notLinkedToRoof) {
unionFind.union(cell, nr);
}
if (!linkedToRoof.isEmpty() || unionFind.isRoof(cell)) {
sum += (unionFind.size(cell) - 1);
}
for (int rr : linkedToRoof) {
unionFind.union(cell, rr);
}
result[i] = sum;
}
return result;
}
private void dfs(int r, int c, int[][] grid, boolean[][] done, UnionFind unionFind) {
done[r][c] = true;
int cell = (r * grid[0].length) + c + 1;
for (int i = 0; i < 4; i++) {
int newR = r + R[i];
int newC = c + C[i];
if (newR >= 0 && newR < grid.length && newC >= 0 && newC < grid[0].length) {
if (grid[newR][newC] == 1 && !done[newR][newC]) {
int newCell = (newR * grid[0].length) + newC + 1;
unionFind.union(cell, newCell);
dfs(newR, newC, grid, done, unionFind);
}
}
}
}
}