Bricks Falling When Hit - Problem

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:

  • It is directly connected to the top of the grid, or
  • At least one other brick in its four adjacent cells is stable

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 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).

Return an array result, where each result[i] is the number of bricks that will fall after the ith erasure is applied.

Note: An erasure may refer to a location with no brick, and if it does, no bricks drop.

Input & Output

Example 1 — Basic Hit
$ Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
Output: [2]
💡 Note: Initially all 4 bricks are stable. After hitting (1,0), only the top-left brick remains stable, so 2 bricks fall (the ones at (1,1) and (1,2)).
Example 2 — Multiple Hits
$ Input: grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
Output: [0,1]
💡 Note: First hit at (1,1) removes an isolated brick, no others fall. Second hit at (1,0) disconnects the remaining bottom brick from the top, so 1 brick falls.
Example 3 — Empty Hit
$ Input: grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,3]]
Output: [0]
💡 Note: Hitting position (1,3) which has no brick, so no bricks fall.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 200
  • grid[i][j] is 0 or 1
  • 1 ≤ hits.length ≤ 4 × 104
  • hits[i].length == 2
  • 0 ≤ xi ≤ m - 1
  • 0 ≤ yi ≤ n - 1

Visualization

Tap to expand
Bricks Falling When Hit Reverse Union-Find Approach INPUT Binary Grid (m x n) 1 0 0 0 1 HIT 1 1 0 = Brick (1) = Empty (0) Hits Array: [[1, 0]] Erase brick at row=1, col=0 Count falling bricks ALGORITHM STEPS 1 Apply All Hits Remove all hit bricks first 2 Build Union-Find Connect stable bricks to top 3 Reverse Process Add bricks back in reverse 4 Count Reconnected Fallen = newly stable - 1 Union-Find Process TOP [0,0] [1,1] [1,2] Dashed = Not connected to top FINAL RESULT Before Hit [1,0]: After Hit [1,0]: [1,1] and [1,2] fall! Output Array: [2] OK - 2 bricks fell Key Insight: Process hits in REVERSE order: Remove all bricks first, then add them back one by one. When adding a brick back, count how many bricks become newly connected to the top row. This avoids recalculating stability repeatedly. Union-Find efficiently tracks connectivity to a virtual "top" node. TutorialsPoint - Bricks Falling When Hit | Reverse Union-Find Approach
Asked in
Google 35 Facebook 28 Amazon 22
28.0K Views
Medium Frequency
~45 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen