← Back to challenges

The Wall is Built from Bricks of Random Lengths

PythonHardalgorithmsarrayscontrol_flowlogic

Instructions

Your function is given a list of rows (the wall). Each row consists of bricks of differing lengths. The sum of brick’s lengths in each row is the same. Imagine a vertical line inside the interval (0, total_row_len), i.e. not at the most left or most right points, inside. This line crosses a certain number of bricks at some rows. If the line passes at a point where one brick finishes and the next one begins, then it does not cross a brick at this row. Find a horizontal point in which the vertical line crosses the least amount of bricks from all the rows. Return the minimum number of crossed bricks.

Examples

least_bricks_cross([
  [1, 2, 2, 1],
  [3, 1, 2],
  [1, 3, 2],
  [2, 4],
  [3, 1, 2],
  [1, 3, 1, 1]
]) ➞ 2

# At point x = 4, the line crosses the first and fourth row from the above.
least_bricks_cross([
  [1], [1], [1]
]) ➞ 3

# At any point within (0, 1) the line will cross all three bricks.
least_bricks_cross([
  [5, 4, 1, 5, 5],
  [1, 2, 3, 2, 2, 4, 4, 2],
  [5, 3, 5, 1, 3, 3],
  [3, 5, 5, 2, 5]
]) ➞ 1

# At point x = 8, the line crosses the first row.

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.