Bricks That Fall After Each Strike
A wall of bricks is stable if each brick connects to the top edge through adjacent bricks. Given a sequence of strikes that remove bricks, report how many bricks fall after each strike.
Problem
Given an m x n binary grid (1 = brick) and an array hits where each hit erases the brick at that cell (no-op if already empty), a brick is stable if it is in the top row or 4-directionally connected to a stable brick. After each hit, return the number of bricks that fall (become unstable) as a result of that hit.
Input
grid: 2D 0/1; hits: array of [row, col] positions, applied in order.
Output
An array result where result[i] is the count of bricks that fall after hits[i].
Constraints
- m == grid.length, n == grid[0].length
- 1 <= m, n <= 200
- 1 <= hits.length <= 40000; hits[i] is within the grid.
Examples
Example 1
Input
grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]
Output
[2]
Removing (1,0) detaches (1,1) and (1,2) from the top, so 2 bricks fall.
Example 2
Input
grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
Output
[0,0]
Removing (1,1) drops nothing; then (1,0) only removes itself.