Ctrl K

How Many Blocks Fall

Hard
A lattice panel of size m × n hangs from the ceiling: panel[i][j] = 1 means the cell holds a block, 0 means it is empty. Row 0 is attached to the ceiling. A block **holds** if it is in row 0 or at least one of its four side neighbours is a block that holds. The panel takes a sequence of strikes strikes[i] = [row, col]. A strike knocks out the block in that cell, if there is one. Right after that, every block that no longer holds falls and disappears from the panel (falling blocks do not land on other blocks). Return an array whose i-th element is the number of blocks that fell after the i-th strike, not counting the struck block itself. A strike may hit an empty cell, in which case nothing falls.
Example 1
panel=[[0, 1, 0], [1, 1, 1], [1, 0, 1]], strikes=[[1, 1]] → [4]

The whole lower pattern hung from the centre block. Once it is knocked out, four blocks fall: the two at the sides of the second row and the two in the third row.

Example 2
panel=[[1, 1], [1, 1]], strikes=[[1, 0], [0, 1], [0, 0]] → [0, 1, 0]

The first strike removes one block and the rest still hold. After the second strike, block (1, 1) has nothing left to hang on: (0, 1) was just knocked out and (1, 0) was knocked out earlier, so it falls. The third strike removes the last block and nothing else falls.

Example 3
panel=[[1, 0], [0, 0]], strikes=[[1, 1]] → [0]

The strike hits an empty cell, so nothing falls.

Constraints
  • 1 ≤ m, n ≤ 200
  • panel[i][j] is 0 or 1
  • 1 ≤ len(strikes) ≤ 4·10⁴
  • 0 ≤ row < m, 0 ≤ col < n
  • All strikes hit different cells
  • Before the first strike every block holds
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCrossing at High Tide HardFill a Single Cell HardCell Levels by Rows and Columns HardLast Dry Crossing Day HardCompress a Grid of Numbers Hard
loading Python…
Input
panel=[[0, 1, 0], [1, 1, 1], [1, 0, 1]], strikes=[[1, 1]]
Expected
[4]

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.