803. Bricks Falling When Hit Hard

@problem@discussion
#Array#Union Find#Matrix



1/**
2 * [803] Bricks Falling When Hit
3 *
4 * You are given an m x n binary grid, where each 1 represents a brick and 0 represents an empty space. A brick is stable if:
5 * 
6 * 	It is directly connected to the top of the grid, or
7 * 	At least one other brick in its four adjacent cells is stable.
8 * 
9 * You are also given an array hits, which is a sequence of erasures we want to apply. Each time we want to erase the brick at the location hits[i] = (rowi, coli). The brick on that location (if it exists) will disappear. Some other bricks may no longer be stable because of that erasure and will fall. Once a brick falls, it is immediately erased from the grid (i.e., it does not land on other stable bricks).
10 * Return an array result, where each result[i] is the number of bricks that will fall after the i^th erasure is applied.
11 * Note that an erasure may refer to a location with no brick, and if it does, no bricks drop.
12 *  
13 * Example 1:
14 * 
15 * Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
16 * Output: [2]
17 * Explanation: Starting with the grid:
18 * [[1,0,0,0],
19 *  [<u>1</u>,1,1,0]]
20 * We erase the underlined brick at (1,0), resulting in the grid:
21 * [[1,0,0,0],
22 *  [0,<u>1</u>,<u>1</u>,0]]
23 * The two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:
24 * [[1,0,0,0],
25 *  [0,0,0,0]]
26 * Hence the result is [2].
27 * 
28 * Example 2:
29 * 
30 * Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
31 * Output: [0,0]
32 * Explanation: Starting with the grid:
33 * [[1,0,0,0],
34 *  [1,<u>1</u>,0,0]]
35 * We erase the underlined brick at (1,1), resulting in the grid:
36 * [[1,0,0,0],
37 *  [1,0,0,0]]
38 * All remaining bricks are still stable, so no bricks fall. The grid remains the same:
39 * [[1,0,0,0],
40 *  [<u>1</u>,0,0,0]]
41 * Next, we erase the underlined brick at (1,0), resulting in the grid:
42 * [[1,0,0,0],
43 *  [0,0,0,0]]
44 * Once again, all remaining bricks are still stable, so no bricks fall.
45 * Hence the result is [0,0].
46 * 
47 *  
48 * Constraints:
49 * 
50 * 	m == grid.length
51 * 	n == grid[i].length
52 * 	1 <= m, n <= 200
53 * 	grid[i][j] is 0 or 1.
54 * 	1 <= hits.length <= 4 * 10^4
55 * 	hits[i].length == 2
56 * 	0 <= xi <= m - 1
57 * 	0 <= yi <= n - 1
58 * 	All (xi, yi) are unique.
59 * 
60 */
61pub struct Solution {}
62
63// problem: https://leetcode.com/problems/bricks-falling-when-hit/
64// discuss: https://leetcode.com/problems/bricks-falling-when-hit/discuss/?currentPage=1&orderBy=most_votes&query=
65
66// submission codes start here
67
68impl Solution {
69    pub fn hit_bricks(grid: Vec<Vec<i32>>, hits: Vec<Vec<i32>>) -> Vec<i32> {
70        vec![]
71    }
72}
73
74// submission codes end
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_803() {
82    }
83}
84


Back
© 2025 bowen.ge All Rights Reserved.