|
1 | 1 | # Time: O(r * c) |
2 | 2 | # Space: O(r * c) |
3 | 3 |
|
| 4 | +# We have a grid of 1s and 0s; the 1s in a cell represent bricks. |
| 5 | +# A brick will not drop if and only if it is directly connected to the top of the grid, |
| 6 | +# or at least one of its (4-way) adjacent bricks will not drop. |
| 7 | +# |
| 8 | +# We will do some erasures sequentially. Each time we want to do the erasure at the location (i, j), |
| 9 | +# the brick (if it exists) on that location will disappear, |
| 10 | +# and then some other bricks may drop because of that erasure. |
| 11 | +# |
| 12 | +# Return an array representing the number of bricks that will drop after each erasure in sequence. |
| 13 | +# |
| 14 | +# Example 1: |
| 15 | +# Input: |
| 16 | +# grid = [[1,0,0,0],[1,1,1,0]] |
| 17 | +# hits = [[1,0]] |
| 18 | +# Output: [2] |
| 19 | +# Explanation: |
| 20 | +# If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2. |
| 21 | +# |
| 22 | +# Example 2: |
| 23 | +# Input: |
| 24 | +# grid = [[1,0,0,0],[1,1,0,0]] |
| 25 | +# hits = [[1,1],[1,0]] |
| 26 | +# Output: [0,0] |
| 27 | +# Explanation: |
| 28 | +# When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. |
| 29 | +# So each erasure will cause no bricks dropping. |
| 30 | +# Note that the erased brick (1, 0) will not be counted as a dropped brick. |
| 31 | +# |
| 32 | +# Note: |
| 33 | +# - The number of rows and columns in the grid will be in the range [1, 200]. |
| 34 | +# - The number of erasures will not exceed the area of the grid. |
| 35 | +# - It is guaranteed that each erasure will be different from any other erasure, and located inside the grid. |
| 36 | +# - An erasure may refer to a location with no brick - if it does, no bricks drop. |
| 37 | + |
4 | 38 | class UnionFind: |
5 | 39 | def __init__(self, n): |
6 | 40 | self.set = range(n+1) |
|
0 commit comments