Bricks Falling When Hit - Problem
Bricks Falling When Hit is a challenging grid simulation problem that models a brick wall's structural integrity. You have an
A brick is stable if:
• It's directly connected to the top row (foundation)
• At least one of its 4 adjacent neighbors is stable
You're given a sequence of
Goal: Return an array where each element represents the number of bricks that fall after each hit.
m × n binary grid where 1 represents a brick and 0 represents empty space.A brick is stable if:
• It's directly connected to the top row (foundation)
• At least one of its 4 adjacent neighbors is stable
You're given a sequence of
hits array where each hit removes a brick at position [row, col]. When a brick is removed, other bricks may become unstable and fall immediately (disappearing from the grid).Goal: Return an array where each element represents the number of bricks that fall after each hit.
Input & Output
example_1.py — Python
$
Input:
grid = [[1,0,0,0],[1,1,1,0],[1,0,1,0],[1,1,1,1]], hits = [[1,0]]
›
Output:
[2]
💡 Note:
After hitting brick at [1,0], the brick at [2,2] becomes disconnected from the top and falls, causing [3,2] to also fall. Total: 2 bricks fall.
example_2.py — Python
$
Input:
grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]
›
Output:
[0,0]
💡 Note:
First hit [1,1] removes a brick but no others fall (0). Second hit [1,0] removes last brick in bottom row, but it was already disconnected (0).
example_3.py — Python
$
Input:
grid = [[1,1,1],[0,1,0],[0,0,0]], hits = [[0,2],[2,0],[0,1],[0,0]]
›
Output:
[0,0,1,0]
💡 Note:
Edge case with hits on empty spaces and sequential disconnections. Only the third hit causes 1 brick to 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 ≤ hits[i][0] < m
- 0 ≤ hits[i][1] < n
- All hits are unique
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code