A wall is drawn as a grid of 0s and 1s, where 1 is a brick. A brick stays up if it is in the top row, or if it touches (up, down, left or right) another brick that stays up. Anything else falls.
You are given a list hits. Each hit [r, c] removes the brick at row r, column c, if there is one. Removing it can leave other bricks with no support; those fall at once and vanish from the grid. A hit on an empty cell does nothing.
Return a list where entry i is how many bricks fall because of hit i. The brick that was hit does not count.
Example 1
Input: grid = [[0, 1, 0], [1, 1, 1]], hits = [[1, 2], [0, 1]] Output: [0, 2] Why: (1, 2) drops nothing; losing (0, 1) drops the 2 below it
2 more examples and the constraints
Example 2
Input: grid = [[1, 1], [0, 1], [1, 1]], hits = [[0, 1]] Output: [3] Why: the right column and the bottom row lose the top
Example 3
Input: grid = [[1, 0], [1, 1]], hits = [[0, 1], [1, 1]] Output: [0, 0] Why: the first hit is empty; the second leaves the left column up
Constraints
- 1 ≤ rows, columns ≤ 200
- Every cell is 0 or 1
- 1 ≤ number of hits ≤ 40,000
- Hits are inside the grid and no cell is hit twice
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Simulate each hitO(h · m · n)
- Union-Find, hits in reverseO((m·n + h) · α)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for a 200 × 200 grid and 40,000 hits | Verdict |
|---|---|---|---|
| Simulate each hit | O(h · m · n) | Correct, far too slow | |
| Union-Find, hits in reverse | O((m·n + h) · α) | Best; one pass backwards |
m × n = grid size, h = number of hits, α = the tiny inverse-Ackermann factor of Union-Find.
Edge cases to test
- Hit on an empty cell. Nothing falls, and the reverse pass must not put a brick there.
- Hit in the top row. Bricks hanging only from that brick fall.
- Brick already fallen. A later hit on a cell whose brick fell earlier finds it empty: 0.
- Restored brick not held up. The top's group does not grow, so clamp the answer to 0, not −1.
- Single row. Every brick is in the top row, so nothing ever falls.
Common mistakes
- Counting the brick that was hit as one of the fallen bricks.
- Restoring a hit that landed on an empty cell, which creates a brick that never existed.
- Joining the top-row bricks to the top only once at the start, and forgetting to do it for restored top-row bricks.
How to talk through it
- 1Clarify. Does the hit brick count? Can a hit land on an empty cell? Does a fallen brick come back?
- 2Give the simulation. Remove, search from the top, sweep the rest; say it costs a full grid per hit.
- 3Flip the time. Deletions are hard for Union-Find, additions are easy, so process the hits backwards.
- 4Add a top node. One virtual node joined to every top-row brick turns 'held up' into 'in the top's group'.
- 5Test out loud. Run a hit on an empty cell and one that drops a whole group.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?