Solving Bricks Falling When Hit in go. Please try yourself first to solve the problem and submit your implementation to LeetCode before looking into solution.

Problem Description

We have a grid of 1s and 0s; the 1s in a cell represent bricks. A brick will not drop if and only if it is directly connected to the top of the grid, or at least one of its (4-way) adjacent bricks will not drop.

Return an array representing the number of bricks that will drop after each erasure in sequence.

Example 1:
grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]
Output: [2]
If we erase the brick at (1, 0), the brick at (1, 1) and (1, 2) will drop. So we should return 2.

Example 2:
grid = [[1,0,0,0],[1,1,0,0]]
hits = [[1,1],[1,0]]
Output: [0,0]
When we erase the brick at (1, 0), the brick at (1, 1) has already disappeared due to the last move. So each erasure will cause no bricks dropping.  Note that the erased brick (1, 0) will not be counted as a dropped brick.


  • The number of erasures will not exceed the area of the grid.
  • It is guaranteed that each erasure will be different from any other erasure, and located inside the grid.
  • An erasure may refer to a location with no brick - if it does, no bricks drop.

See the full details of the problem Bricks Falling When Hit at LeetCode

Originally posted at: