554. Brick Wall
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method Explanation
The problem asks us to find a vertical line that crosses the fewest bricks. A crucial observation is that a vertical line does not cross a brick if it passes through an edge between two bricks.
Solution
Brick Wall in Python
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
# Use a dictionary (hash map) to store the frequency of each edge position.
# The key is the edge position (prefix sum of widths), and the value is the count.
edge_counts = {}
num_rows = len(wall)
max_edges = 0 # To store the maximum number of aligned edges
# Iterate through each row of the wall
for row in wall:
current_width = 0
# Iterate through the bricks in the current row, but exclude the last brick.
# We don't consider the rightmost edge of the wall.
for i in range(len(row) - 1):
brick_width = row[i]
current_width += brick_width
edge_counts[current_width] = edge_counts.get(current_width, 0) + 1
# Update the maximum number of aligned edges found so far
max_edges = max(max_edges, edge_counts[current_width])
# The minimum number of bricks crossed is the total number of rows
# minus the maximum number of edges that align at any single position.
return num_rows - max_edges
Brick Wall in Java
class Solution {
public int leastBricks(List<List<Integer>> wall) {
// Use a HashMap to store the frequency of each edge position.
// The key is the edge position (prefix sum of widths), and the value is the count.
// We use Long for the key to prevent potential integer overflow.
Map<Long, Integer> edgeCounts = new HashMap<>();
int numRows = wall.size();
int maxEdges = 0; // To store the maximum number of aligned edges
// Iterate through each row of the wall
for (List<Integer> row : wall) {
long currentWidth = 0;
// Iterate through the bricks in the current row, but exclude the last brick.
// We don't consider the rightmost edge of the wall.
for (int i = 0; i < row.size() - 1; i++) {
currentWidth += row.get(i);
int count = edgeCounts.getOrDefault(currentWidth, 0) + 1;
edgeCounts.put(currentWidth, count);
// Update the maximum number of aligned edges found so far
maxEdges = Math.max(maxEdges, count);
}
}
// The minimum number of bricks crossed is the total number of rows
// minus the maximum number of edges that align at any single position.
return numRows - maxEdges;
}
}
Brick Wall in C++
class Solution {
public:
int leastBricks(vector<vector<int>>& wall) {
// Use a hash map to store the frequency of each edge position.
// The key is the edge position (prefix sum of widths), and the value is the count.
// We use long long for the key to prevent potential integer overflow, as the sum of widths can be large.
unordered_map<long long, int> edge_counts;
int n = wall.size(); // Total number of rows
int max_edges = 0; // To store the maximum number of aligned edges
// Iterate through each row of the wall
for (const auto& row : wall) {
long long current_width = 0;
// Iterate through the bricks in the current row, but exclude the last brick.
// We don't consider the rightmost edge of the wall.
for (int i = 0; i < row.size() - 1; ++i) {
current_width += row[i];
edge_counts[current_width]++;
// Update the maximum number of aligned edges found so far
max_edges = max(max_edges, edge_counts[current_width]);
}
}
// The minimum number of bricks crossed is the total number of rows
// minus the maximum number of edges that align at any single position.
return n - max_edges;
}
};
Brick Wall in TypeScript
function leastBricks(wall: number[][]): number {
// Use a Map to store the frequency of each edge position.
// The key is the edge position (prefix sum of widths), and the value is the count.
const edgeCounts: Map<number, number> = new Map();
const numRows = wall.length;
let maxEdges = 0; // To store the maximum number of aligned edges
// Iterate through each row of the wall
for (const row of wall) {
let currentWidth = 0;
// Iterate through the bricks in the current row, but exclude the last brick.
// We don't consider the rightmost edge of the wall.
for (let i = 0; i < row.length - 1; i++) {
currentWidth += row[i];
const count = (edgeCounts.get(currentWidth) || 0) + 1;
edgeCounts.set(currentWidth, count);
// Update the maximum number of aligned edges found so far
maxEdges = Math.max(maxEdges, count);
}
}
// The minimum number of bricks crossed is the total number of rows
// minus the maximum number of edges that align at any single position.
return numRows - maxEdges;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.