#554

Brick Wall

Medium HashMap of edge positions

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A wall is made of rows of bricks of different widths (all rows equally wide). Draw one vertical line top to bottom crossing the fewest bricks; lines along brick edges don't cross them. Return the minimum crossed.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: where should a vertical line go to cross the fewest bricks?

Why this works: crossing fewest bricks = passing through the most brick edges. Count the edge positions across rows.

Example: rows [1,2,2,1], [3,1,2], [1,3,2] …

edge at position 4 appears in most rowsanswer = rows - that count

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Including the right edge of the wall, which every row shares — it's not allowed.

Solve on LeetCode ↗
Complexity
  • Time: O(total bricks).
  • Space: O(number of distinct edges).
Topic: Hashing
🎬 Episodes 37–39: video coming soon