Bricks Falling When Hit

Given a grid of bricks and a list of hits, return how many bricks fall after each hit. DSU is used in reverse: restore bricks from the last hit to the first while maintaining connectivity to the roof.

Input Format

grid = brick matrix, hits = cells removed one by one

Output Format

number of bricks falling after each hit

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0]]

Output:

[2]

Explanation:

Removing the brick at (1,0) causes two additional bricks to fall.

Example 2:

Input:

grid = [[1,0,0,0],[1,1,1,0]]
hits = [[1,0],[1,1]]

Output:

[2,0]

Explanation:

The first hit drops two bricks; the second hit drops none.

Example 3:

Input:

grid = [[1]]
hits = [[0,0]]

Output:

[0]

Explanation:

A single brick removed directly does not cause any extra bricks to fall.

Loading...
Bricks Falling When Hit - Union Find DSA Problem