## Similar Problems

Similar Problems not available

# Bricks Falling When Hit - Leetcode Solution

## Companies:

LeetCode: Bricks Falling When Hit Leetcode Solution

Difficulty: Hard

Topics: union-find matrix array

The Bricks Falling When Hit problem on LeetCode is a problem about simulating the aftermath of hitting a brick from a wall. Given a wall of bricks represented by a 2D array of 1s and 0s, where 1s represent bricks and 0s represent empty spaces, and a sequence of hits represented by an array of coordinates (row, column), the task is to determine the number of bricks that will fall after each hit.

The solution to this problem involves a series of steps. We will give a detailed solution below:

Step 1: Remove the hits Firstly, for each hit in the sequence, remove the brick at the specified coordinate. This is done by setting the value of the brick at the specified coordinate to 0.

Step 2: Find the connected components Secondly, find the connected components of the wall using a recursive depth-first search (DFS) algorithm. A connected component is a group of adjacent bricks that are still standing after all the hits have been applied. A DFS algorithm is used because it enables us to efficiently traverse all the adjacent bricks of a given brick.

Step 3: Determine which bricks will fall Thirdly, for each hit, determine which bricks will fall. A brick will fall if it is part of a connected component that is not connected to the top row of the wall. This is because if a connected component is not connected to the top row, then it means that it is not supported by any other bricks and therefore, will fall.

Step 4: Return the number of bricks that will fall Lastly, return the number of bricks that will fall for each hit.

In summary, the solution involves performing a series of steps: removing the hits, finding the connected components, determining which bricks will fall, and returning the number of bricks that will fall. Each step plays an important role in solving the problem, and used together, they form an efficient algorithm for simulating the aftermath of hitting a brick from a wall.

## Bricks Falling When Hit Solution Code

`1`