Advertisement

Brick Wall - LeetCode 554 Solution

Brick Wall - Complete Solution Guide

Brick Wall is LeetCode problem 554, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There is a rectangular brick wall in front of you with n rows of bricks. The i th row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same. Draw a vertical line from the top to the bottom and cross the least bricks. If your line goes through the edge of a brick, then the brick is not considered as crossed. You cannot draw a line just along one of the two vertical edges of the wall, in which case the line w

Detailed Explanation

The problem asks us to find the minimum number of bricks crossed by a vertical line drawn from the top to the bottom of a brick wall. The wall is represented by a 2D array where each row contains the widths of the bricks in that row. The key rule is that if the line goes through the edge of a brick (i.e., the boundary between two bricks), the brick is *not* considered crossed. We must find the vertical line that crosses the fewest bricks, avoiding drawing the line along the edges of the wall.

Solution Approach

The solution iterates through each row of the wall, calculating the cumulative width at each gap between bricks. It uses a hash table (`gaps_count`) to store the frequency of each gap position. After processing all rows, the algorithm finds the gap position with the highest frequency (`max_gaps`). The minimum number of crossed bricks is then calculated as the total number of rows minus `max_gaps`. This works because `max_gaps` represents the number of rows where the chosen vertical line avoids crossing a brick.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash table (or dictionary) `gaps_count` to store the number of times each gap position appears.
  2. Step 2: Initialize `max_gaps` to 0. This variable will keep track of the maximum number of times any gap position appears.
  3. Step 3: Iterate through each row in the `wall`.
  4. Step 4: For each row, initialize `position` to 0. This variable will track the cumulative width as we iterate through the bricks in the row.
  5. Step 5: Iterate through the bricks in the row *except* for the last brick. This is because a gap after the last brick is not a valid place to draw the line.
  6. Step 6: Add the current brick's width to `position` to update the cumulative width.
  7. Step 7: Update the `gaps_count` hash table. Increment the count for the current `position` (gap). If the gap doesn't exist, add it with a count of 1.
  8. Step 8: Update `max_gaps` to be the maximum value between the current `max_gaps` and the count of the current `position` in `gaps_count`.
  9. Step 9: After processing all rows, return the number of rows in the wall minus `max_gaps`. This is the minimum number of bricks crossed.
  10. Step 10: C specific version of solution avoids usage of hashmap to improve performance. Here number of possible gaps is limited so we use fixed-size array to keep track of gap counts

Key Insights

  • Insight 1: The problem is equivalent to finding the horizontal gap between bricks that appears most frequently across all rows. A line drawn through this gap will cross the fewest bricks.
  • Insight 2: A hash table (or dictionary) is essential to efficiently count the occurrences of each gap position.
  • Insight 3: The width of the wall is constant across all rows, so we only need to consider gap positions up to this width.
  • Insight 4: We only need to calculate the cumulative width up to the second-to-last brick in each row because a gap after the last brick is equivalent to a line drawn on the edge of the wall which is not allowed.

Complexity Analysis

Time Complexity: O(R * B)

Space Complexity: O(W)

Topics

This problem involves: Array, Hash Table.

Companies

Asked at: Agoda, Trexquant.