Find the vertical line that crosses the fewest bricks in a wall

Quick Overview

Given a brick wall as rows of brick widths, find where a vertical line should go so it crosses the fewest bricks, and return how many it crosses. The problem tests whether you can count shared edge positions with a hash map and handle the wall's outer edges and single-brick rows correctly.

Find the vertical line that crosses the fewest bricks in a wall

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A rectangular brick wall has `n` rows. Every brick is one unit tall, bricks in the same row can have different widths, and every row has the same total width. The wall is given as a list of rows, where `wall[i]` lists the widths of the bricks in row `i` from left to right. You draw one vertical line from the top of the wall to the bottom. If the line runs exactly along the edge between two bricks, neither of those bricks counts as crossed. Choose where to draw the line so that it crosses as few bricks as possible, and return that number of crossed bricks. The line may not lie on the left or right outer edge of the wall, where it would trivially cross no bricks. ### Function Signature ```python def least_bricks(wall: list[list[int]]) -> int: ``` ### Rules - Horizontal positions are measured from the left edge of the wall, which is position 0. If the total width is `W`, the line must be at a position strictly between 0 and `W`. - A row adds one crossed brick to the count unless the line's position is an edge between two of that row's bricks. - The output is the minimum number of crossed bricks. Several positions may reach that minimum; the position itself is not returned. ### Constraints - `n == len(wall)` - `1 <= n <= 10^4` - `1 <= len(wall[i]) <= 10^4` - `1 <= sum(len(wall[i]) for every row i) <= 2 * 10^4` - `sum(wall[i])` is the same for every row - `1 <= wall[i][j] <= 2^31 - 1` ### Examples ```text Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]] Output: 2 ``` The wall is 6 units wide, and position 4 is a brick edge in the second, third, fifth and sixth rows. A line there crosses only two bricks: the one covering 3 to 5 in the first row and the one covering 2 to 6 in the fourth row. ```text Input: wall = [[1],[1],[1]] Output: 3 ``` Each row is a single brick, so no row has an edge inside the wall. Any allowed line crosses all three bricks. ```text Input: wall = [[1,1],[2],[1,1]] Output: 1 ``` Position 1 is an edge in the first and third rows. A line there crosses only the width-2 brick in the middle row.

Overview: Given a brick wall as rows of brick widths, find where a vertical line should go so it crosses the fewest bricks, and return how many it crosses. The problem tests whether you can count shared edge positions with a hash map and handle the wall's outer edges and single-brick rows correctly.

Oracle logo
Oracle
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A rectangular brick wall has n rows. Every brick is one unit tall, bricks in the same row can have different widths, and every row has the same total width. The wall is given as a list of rows, where wall[i] lists the widths of the bricks in row i from left to right.

You draw one vertical line from the top of the wall to the bottom. If the line runs exactly along the edge between two bricks, neither of those bricks counts as crossed. Choose where to draw the line so that it crosses as few bricks as possible, and return that number of crossed bricks.

The line may not lie on the left or right outer edge of the wall, where it would trivially cross no bricks.

Function Signature

def least_bricks(wall: list[list[int]]) -> int:

Rules

  • Horizontal positions are measured from the left edge of the wall, which is position 0. If the total width is W , the line must be at a position strictly between 0 and W .
  • A row adds one crossed brick to the count unless the line's position is an edge between two of that row's bricks.
  • The output is the minimum number of crossed bricks. Several positions may reach that minimum; the position itself is not returned.

Constraints

  • n == len(wall)
  • 1 <= n <= 10^4
  • 1 <= len(wall[i]) <= 10^4
  • 1 <= sum(len(wall[i]) for every row i) <= 2 * 10^4
  • sum(wall[i]) is the same for every row
  • 1 <= wall[i][j] <= 2^31 - 1

Examples

Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
Output: 2

The wall is 6 units wide, and position 4 is a brick edge in the second, third, fifth and sixth rows. A line there crosses only two bricks: the one covering 3 to 5 in the first row and the one covering 2 to 6 in the fourth row.

Input: wall = [[1],[1],[1]]
Output: 3

Each row is a single brick, so no row has an edge inside the wall. Any allowed line crosses all three bricks.

Input: wall = [[1,1],[2],[1,1]]
Output: 1

Position 1 is an edge in the first and third rows. A line there crosses only the width-2 brick in the middle row.