Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
There is a rectangular brick wall in front of you with n rows of bricks. The ith 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 will obviously cross no bricks.
Given the 2D array wall that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.
Example 1:
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]] Output: 2
Example 2:
Input: wall = [[1],[1],[1]] Output: 3
Constraints:
n == wall.length1 <= n <= 1041 <= wall[i].length <= 1041 <= sum(wall[i].length) <= 2 * 104sum(wall[i]) is the same for each row i.1 <= wall[i][j] <= 231 - 1Problem summary: There is a rectangular brick wall in front of you with n rows of bricks. The ith 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 will obviously cross no bricks. Given the 2D array wall that contains the information about the wall, return the minimum number of crossed bricks after drawing such a vertical line.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
[[1],[1],[1]]
number-of-ways-to-build-sturdy-brick-wall)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #554: Brick Wall
class Solution {
public int leastBricks(List<List<Integer>> wall) {
Map<Integer, Integer> cnt = new HashMap<>();
for (var row : wall) {
int s = 0;
for (int i = 0; i + 1 < row.size(); ++i) {
s += row.get(i);
cnt.merge(s, 1, Integer::sum);
}
}
int mx = 0;
for (var x : cnt.values()) {
mx = Math.max(mx, x);
}
return wall.size() - mx;
}
}
// Accepted solution for LeetCode #554: Brick Wall
func leastBricks(wall [][]int) int {
cnt := map[int]int{}
for _, row := range wall {
s := 0
for _, x := range row[:len(row)-1] {
s += x
cnt[s]++
}
}
mx := 0
for _, x := range cnt {
mx = max(mx, x)
}
return len(wall) - mx
}
# Accepted solution for LeetCode #554: Brick Wall
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
cnt = Counter()
for row in wall:
s = 0
for x in row[:-1]:
s += x
cnt[s] += 1
return len(wall) - max(cnt.values(), default=0)
// Accepted solution for LeetCode #554: Brick Wall
use std::collections::HashMap;
impl Solution {
pub fn least_bricks(wall: Vec<Vec<i32>>) -> i32 {
wall.len() as i32
- wall
.into_iter()
.map(|row| {
let mut prefix: Vec<_> = row
.into_iter()
.scan(0, |sum, x| {
*sum += x;
Some(*sum)
})
.collect();
prefix.pop();
prefix
})
.flatten()
.fold(HashMap::from([(0, 0)]), |mut acc, x| {
acc.entry(x).and_modify(|cnt| *cnt += 1).or_insert(1);
acc
})
.into_iter()
.map(|(_, v)| v)
.max()
.unwrap()
}
}
// Accepted solution for LeetCode #554: Brick Wall
function leastBricks(wall: number[][]): number {
const cnt: Map<number, number> = new Map();
for (const row of wall) {
let s = 0;
for (let i = 0; i + 1 < row.length; ++i) {
s += row[i];
cnt.set(s, (cnt.get(s) || 0) + 1);
}
}
const mx = Math.max(...cnt.values(), 0);
return wall.length - mx;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.