Bricks Falling When Hit
master · 1800 · lc hard +32 · verified · 37% accepted · 1,202 likes · top 15%
Description
You are given an m x n binary grid where 1 represents a brick and 0 represents empty space. A brick is considered stable when:
- It is directly attached to the top edge of the grid, or
- At least one adjacent brick (in four directions) is itself stable.
You are also given an array hits specifying a sequence of bricks to erase. Erasing the brick at hits[i] = (rowi, coli) removes it (if present), and any bricks that then lose stability will also fall and be erased immediately.
Return an array result where result[i] is the number of bricks that fall after the ith erasure.
Note: if a hit location contains no brick, no bricks fall.
Example 1:
Example 2:
Code