Leetcode Problem 803. Bricks Falling When Hit

803. Bricks Falling When Hit

Leetcode Solutions

Reverse Time and Union-Find

  1. Create a copy of the grid after all hits have been applied, marking the hit bricks as absent.
  2. Initialize a DSU structure and connect all bricks that are not hit and are stable.
  3. Connect the top row bricks to a virtual 'roof' node in the DSU.
  4. Iterate through the hits in reverse order, adding back each brick if it was originally there.
  5. For each added brick, connect it to its stable neighbors and update the DSU.
  6. If adding the brick connects new bricks to the roof, count the number of bricks that became stable and add it to the result.
  7. Reverse the result array to match the original order of hits.
  8. Return the result array.
UML Thumbnail

Depth-First Search (DFS) Post-Processing

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...