Description,
There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. The bricks have the same height but different width. You want to draw a vertical line from the top to the bottom and cross the least bricks.

The brick wall is represented by a list of rows. Each row is a list of integers representing the width of each brick in this row from left to right.

If your line go through the edge of a brick, then the brick is not considered as crossed. You need to find out how to draw the line to cross the least bricks and return the number of crossed bricks.

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.

Example:

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

Explanation:
img

O(n) time complexity O(width) space complexity solution

Algorithm:
In this approach, we make use of a HashMap mapmap which is used to store entries in the form: (sum,count). Here, sum refers to the cumulative sum of the bricks’ widths encountered in the current row, and countcount refers to the number of times the corresponding sum is obtained. Thus, sum in a way, represents the positions of the bricks’s boundaries relative to the leftmost boundary – means this position can bypass the current brick in the row.

public class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        HashMap<Integer, Integer> hm = new HashMap<>();
        for (List<Integer> row : wall) {
            int sum = 0;
            for (int j=0; j<row.size()-1; j++) {
                sum += row.get(j);
                if (hm.containsKey(sum)) {
                    hm.put(sum, hm.get(sum)+1);
                } else {
                    hm.put(sum, 1);
                }
            }
        }
        
        int maxBrickBypassed = 0;
        for (int brickBypassed : hm.values()) {
            maxBrickBypassed = Math.max(maxBrickBypassed, brickBypassed);
        }
        return wall.size() - maxBrickBypassed;
    }
}

imgRuntime: 42ms

A slightly brute-force way, is to rebuild the matrix. However it is not necessary, and will timeout.

class Solution(object):
    def leastBricks(self, wall):
        """
        :type wall: List[List[int]]
        :rtype: int
        """
        ret = 0
        newWall = []
        hm = dict()
        for i in xrange(len(wall)):
            level = []
            fill = 2
            for brickSize in wall[i]:
                fill = 1 + 2 - fill # flip the fill 
                for _ in xrange(brickSize):
                    level.append(fill)
            newWall.append(level)
        #print newWall
        
        for i in xrange(len(newWall)):
            for j in xrange(len(newWall[0])-1):
                if j not in hm:
                    hm[j] = 0
                    hm[j] += abs(newWall[i][j] - newWall[i][j+1])
                else:
                    hm[j] += abs(newWall[i][j] - newWall[i][j+1])
        
        for v in hm.itervalues():
            #print v
            ret = max(ret, v)
        return len(newWall) - ret