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.

&nbsp;
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


&nbsp;
Constraints:


	n == wall.length
	1 &lt;= n &lt;= 104
	1 &lt;= wall[i].length &lt;= 104
	1 &lt;= sum(wall[i].length) &lt;= 2 * 104
	sum(wall[i]) is the same for each row i.
	1 &lt;= wall[i][j] &lt;= 231 - 1


class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
        m = len(wall)
        ctr = {}
        res = m
        for i in range(m):
            n = len(wall[i])
            curr = 0
            for j in range(n - 1):
                curr += wall[i][j]
                x = ctr.get(curr, m) - 1
                ctr[curr] = x
                res = min(res, x)
        return res
class Solution {
    public int leastBricks(List<List<Integer>> wall) 
    {
        HashMap<Integer, Integer> edge_frequency = new HashMap<>(); //HashMap to store the number of common edges among the rows
        int max_frequency = 0; //Variable to store the frequency of most occuring edge
        
        for(int row=0; row<wall.size(); row++) //Iterating through each row
        {
            int edge_postion = 0; //Variable to store different edge postion
            
            for(int brick_no=0; brick_no<wall.get(row).size()-1; brick_no++) //Iterating through each brick inside a row
            {
                int current_brick_length = wall.get(row).get(brick_no); //Length of the current brick
                edge_postion = edge_postion + current_brick_length; //Next Edge Position = Previous Edge Position + Current Brick's Length
                edge_frequency.put(edge_postion,edge_frequency.getOrDefault(edge_postion,0)+1); //Incrementing the Frequency of just calculated Edge Postion
                max_frequency = Math.max(edge_frequency.get(edge_postion),max_frequency); //Comparing the "Frequency of just calculated Edge Postion" with "Max Frequency seen till now" & storing whichever is greater.
            }
        }
        return wall.size() - max_frequency; // returning (Number of Bricks Crossed by Line) i.e. (Number of Rows in Wall - Frequency of Most Occuring Edge)
    }
}
class Solution {
public:
    int leastBricks(vector<vector<int>>& wall) {
        vector<unordered_set<int>> v;
        for(auto i : wall) {
            int cs = 0;
            unordered_set<int> st;
            for(int j=0; j<i.size() - 1; j++) {
                cs += i[j];
                st.insert(cs);
            }
            v.push_back(st);
        }
        
        unordered_map<int, int> m;
        for(auto i : v) {
            for(auto j : i) m[j]++;
        }
        
        int mxfr = INT_MIN, mxel;
        for(auto i : m) {
            if(i.second > mxfr) mxel = i.first, mxfr = i.second;
        }
        
        int ans = 0;
        for(auto s : v) {
            if(s.find(mxel) == s.end()) ans++;
        }
        return ans;
    }
};
var leastBricks = function(wall) {
	const hash = wall.reduce((map, row) => {
		let sum = 0;

		for (let index = 0; index < row.length - 1; index++) {
			sum += row[index];
			const hashCount = map.get(sum) ?? 0;
			map.set(sum, hashCount + 1);
		}
		return map;
	}, new Map());

	let result = wall.length;
	hash.forEach(value => result = Math.min(result, wall.length - value));

	return result;
};
Brick Wall