Brick Wall LeetCode Solution

Last updated on October 5th, 2024 at 04:21 pm

Here, We see Brick Wall LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Hash Table

Companies

Facebook

Level of Question

Medium

Brick Wall LeetCode Solution

Brick Wall LeetCode Solution

Problem Statement

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.

cutwall grid

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

1. Brick Wall LeetCode Solution C++

class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
    unordered_map<int, int> edge_frequency;
        int max_frequency = 0;
        for(int row=0; row<wall.size(); row++)
        {
            int edge_postion = 0;
            for(int brick_no=0; brick_no< wall[row].size() -1; brick_no++)
            { 
                int current_brick_length = wall[row][brick_no];
                edge_postion = edge_postion + current_brick_length ;
                edge_frequency[edge_postion]++;
                max_frequency = max(edge_frequency[edge_postion],max_frequency);
            }
        }
        return wall.size() - max_frequency;        
    }
};

2. Brick Wall LeetCode Solution Java

class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        var edge_frequency=new HashMap<Integer,Integer>();
        int max_frequency = 0;
        for(int row=0; row<wall.size(); row++)
        {
            int edge_postion = 0;
            for(int brick_no=0; brick_no< wall.get(row).size() -1; brick_no++)
            { 
                int current_brick_length = wall.get(row).get(brick_no);
                edge_postion = edge_postion + current_brick_length ;
                edge_frequency.put(edge_postion,edge_frequency.getOrDefault(edge_postion,0)+1);
                max_frequency = Math.max(edge_frequency.get(edge_postion),max_frequency);
            }
        }
        return wall.size() - max_frequency;        
    }
}

3. Brick Wall LeetCode Solution JavaScript

var leastBricks = function(wall) {
    let freq = new Map(), best = 0
    for (let i = 0; i < wall.length; i++) {
        let row = wall[i], rowSum = row[0]
        for (let j = 1; j < row.length; j++) {
            freq.set(rowSum, (freq.get(rowSum) || 0) + 1)
            rowSum += row[j]
        } 
    }
    for (let [k,v] of freq)
        if (v > best) best = v
    return wall.length - best
};

4. Brick Wall LeetCode Solution Python

class Solution(object):
    def leastBricks(self, wall):
        d = collections.defaultdict(int)
        for line in wall:
            i = 0
            for brick in line[:-1]:
                i += brick
                d[i] += 1
        # print len(wall), d
        return len(wall)-max(d.values()+[0])
Scroll to Top