Brick Wall
Problem
Given a list of rows representing a brick wall where each row is a list of brick widths, return the minimum number of bricks that a vertical line crosses when drawn from top to bottom between the bricks.
- 1 ≤ wall.length ≤ 10⁴
- 1 ≤ wall[i].length ≤ 10⁴
- 1 ≤ sum(wall[i]) ≤ 2³¹ - 1
- The sum of widths in each row is the same
Example
wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]2The algorithm counts the positions of brick edges (excluding the rightmost edge) across all rows. For example, the edge positions for the first row are at 1, 3, and 5. The position with the most aligned edges is 3, where 4 rows have edges aligned. Drawing the line at position 3 crosses the fewest bricks, which is total rows (6) minus 4 aligned edges = 2.
Approach
Straightforward Solution
A brute-force approach would check every possible vertical line position across the wall width and count how many bricks it crosses by scanning each row, resulting in O(n * m) time where n is the number of rows and m is the number of bricks per row. This is inefficient for large inputs.
Core Observation
The vertical line crosses a brick if it does not pass through a brick edge. Therefore, minimizing crossed bricks is equivalent to maximizing the number of aligned edges the line passes through.
Path to Optimal
The key insight is to track the cumulative edge positions of bricks in each row (excluding the last brick to avoid the wall's right edge). By counting how many rows have an edge at each position, the position with the maximum count corresponds to the line crossing the fewest bricks. This transforms the problem into a frequency counting problem using a hash map.
Optimal Approach
Iterate through each row, compute the prefix sums of brick widths to find edge positions, and update a hash map counting how many rows have edges at each position. The answer is the total number of rows minus the maximum edge count found. If no edges align (empty map), the line crosses all bricks.
Time
O(n * m)
Each row is processed once, and each brick except the last is visited to compute prefix sums and update the hash map. Hash map operations are O(1) on average.
Space
O(n * m)
In the worst case, all edge positions are unique, requiring storage proportional to the total number of bricks minus the number of rows. This space is necessary to track edge frequencies.
Pattern Spotlight
Hash Maps (Frequency Counting of Prefix Sums)
When minimizing crossings or overlaps along a linear dimension, transform the problem into counting aligned boundaries using prefix sums and hash maps to find the position with maximum alignment efficiently.
Solution
| 1 | class Solution: |
| 2 | def leastBricks(self, wall: list[list[int]]) -> int: |
| 3 | edge_counts = {} |
| 4 | |
| 5 | for row in wall: |
| 6 | edge_position = 0 |
| 7 | |
| 8 | for brick_width in row[:-1]: |
| 9 | edge_position += brick_width |
| 10 | edge_counts[edge_position] = edge_counts.get(edge_position, 0) + 1 |
| 11 | |
| 12 | max_aligned_edges = 0 |
| 13 | |
| 14 | for count in edge_counts.values(): |
| 15 | max_aligned_edges = max(max_aligned_edges, count) |
| 16 | |
| 17 | return len(wall) - max_aligned_edges |
Step-by-Step Solution
Accumulate Edge Positions Across Rows Using Prefix Sums
| 3 | edge_counts = {} |
| 5 | for row in wall: |
| 6 | edge_position = 0 |
| 8 | for brick_width in row[:-1]: |
| 9 | edge_position += brick_width |
| 10 | edge_counts[edge_position] = edge_counts.get(edge_position, 0) + 1 |
Objective
To compute and count the positions of brick edges (excluding the rightmost edge) across all rows to identify where edges align.
Key Insight
By iterating through each brick in a row except the last, summing their widths to find the edge positions, and updating a hash map with counts of how many rows have edges at each position, the algorithm efficiently aggregates alignment information. This approach avoids scanning every possible vertical line position and leverages prefix sums to transform the problem into a frequency counting task.
Interview Quick-Check
Core Logic
Use prefix sums to find edge positions and a hash map to count how many rows share each edge position.
State & Boundaries
Exclude the last brick in each row to avoid counting the wall's right edge.
Common Pitfalls & Bugs
Forgetting to exclude the last brick leads to incorrect counts and invalid line positions.
Determine Maximum Edge Alignment and Compute Result
| 12 | max_aligned_edges = 0 |
| 14 | for count in edge_counts.values(): |
| 15 | max_aligned_edges = max(max_aligned_edges, count) |
| 17 | return len(wall) - max_aligned_edges |
Objective
To find the edge position with the maximum alignment and calculate the minimum number of bricks crossed by the vertical line.
Key Insight
The position with the highest count of aligned edges corresponds to the vertical line crossing the fewest bricks. Subtracting this maximum count from the total number of rows yields the minimum number of bricks crossed. If no edges align, the line crosses all bricks. This final step consolidates the frequency data into the problem's required output.
Interview Quick-Check
Core Logic
Find the maximum frequency of aligned edges and subtract it from the total number of rows to get the minimum bricks crossed.
State & Boundaries
If no edges align (empty map), the line crosses all bricks.
Complexity
This step runs in O(n) time, iterating over the hash map values once.
Line Analysis
This solution has 3 Critical lines interviewers watch for.
return len(wall) - max_aligned_edges
Return the minimum number of bricks crossed by subtracting max aligned edges from total rows.
Subtracting the maximum edge alignment from the total rows yields the minimal number of bricks the vertical line crosses, solving the problem.
for brick_width in row[:-1]:
Iterate through each brick in the row except the last one.
Excluding the last brick avoids counting the wall's right edge, which is not a valid line position.
edge_counts[edge_position] = edge_counts.get(edge_position, 0) + 1
Increment the count of rows with an edge at the current position in the hash map.
Counting how many rows share an edge at this position identifies potential line placements that minimize brick crossings.
edge_counts = {}
Initialize a hash map to count edge positions across rows.
This data structure enables O(1) average-time updates and lookups for edge counts, transforming the problem into a frequency counting task.
max_aligned_edges = max(max_aligned_edges, count)
Update the maximum aligned edges if the current count is higher.
Maintaining the maximum count ensures the algorithm correctly identifies the optimal line position.
edge_position += brick_width
Update the cumulative edge position by adding the current brick's width.
Prefix sums track the position of each brick edge relative to the wall's left boundary, enabling alignment detection.
for count in edge_counts.values():
Iterate over all edge counts to find the maximum alignment.
Scanning all counts identifies the position where the vertical line crosses the fewest bricks by maximizing edge alignment.
edge_position = 0
Initialize the cumulative edge position for the current row.
Starting at zero ensures prefix sums correctly represent positions from the left edge of the wall.
max_aligned_edges = 0
Initialize a variable to track the maximum number of aligned edges.
This variable will store the highest frequency of aligned edges found, which is critical for computing the final answer.
for row in wall:
Iterate over each row in the wall.
Processing each row individually allows accumulation of edge positions to identify alignment across rows.
Test Your Understanding
Why do we exclude the last brick in each row when counting edge positions?
Because the last brick's right edge coincides with the wall's right boundary, where the line cannot be drawn. Including it would incorrectly count the wall's edge as a valid line position.
Related Problems
Hash Maps pattern
Don't just read it. Drill it.
Reconstruct Brick Wall from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Brick Wall drill