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: 




Note:

The width sum of bricks in different rows are the same and won't exceed INT_MAX.
The number of bricks in each row is in range [1,10,000]. The height of wall is in range [1,10,000]. Total number of bricks of the wall won't exceed 20,000.

Difficulty: interview

Starter code:

class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
        

Solutions:
class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         d = {}
         for i in wall:
             suma = 0
             for j in range(len(i)-1):
                 suma += i[j]
                 if suma in d:
                     d[suma] += 1
                 else:
                     d[suma] = 1
         if len(d) == 0:
             return len(wall)
         return len(wall) - max(d.values())


class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         
         x_to_edge_dict = {}
         rows = len(wall)
         cols = 0
         if rows > 0:
             cols = len(wall[0])
             
         for row in range(rows):
             last_pos = 0
             for brick in wall[row][:-1]: # -1 to ignore end of last brick
                 end = last_pos + brick
                 if end not in x_to_edge_dict:
                     x_to_edge_dict[end] = 1
                 else:
                     x_to_edge_dict[end] += 1
                 last_pos = end
         
         #print(x_to_edge_dict)
         if x_to_edge_dict == {}:
             return rows
         else:
             return rows - max(x_to_edge_dict.values())
                 
         
                 
                 


class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         # use a hash to store edge location => number of bricks whose edge is on it
         # then the result is total_height - edge that has max of bricks
         if not wall: return 0
         hsh = collections.defaultdict(int)
         for row in wall:
             distance_from_left = 0
             # Notice you cannot go to the last one because the last edge will all be the width of the wall
             for brick_width in row[:-1]:
                 distance_from_left += brick_width
                 hsh[distance_from_left] += 1
         # notice hsh might be empty
         return len(wall) - max(list(hsh.values())+[0])
                 
 
         


class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         edges = collections.defaultdict(int)
         length = sum(wall[0])
         for row in wall:
             total = 0
             for brick in row:
                 total += brick
                 if total < length:
                     edges[total] += 1
         if not edges:
             return len(wall)
         return len(wall) - max(edges.values())


class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         if not wall:
             return 0
         
         width = sum(wall[0])
         length = len(wall)
         columns = collections.defaultdict(lambda: length)
         
         for r in range(len(wall)):
             col = 0
             for c in range(len(wall[r])-1):
                 col += wall[r][c]
                 columns[col] -= 1
         
         return length if len(columns) == 0 else min(columns.values())


class Solution:
     def leastBricks(self, wall):
         """
         :type wall: List[List[int]]
         :rtype: int
         """
         """
         Time O(r*c) because we iterate through the wall once and do O(1) work for each element
         in the wall.
         Space O(c) because in the worst case, our hashmap will have length c.
         """
         if not wall:
             return 0
         
         """
         The maximum number of bricks we have to intersect is the equal to the number of rows,
         in the case that we have to intersect a brick at every row.
         columns will be a map from a column to the number of bricks that would be intersected
         if we drew a line through that column. For now, we assume that every column will intersect
         the maximum number of bricks, which is the number of rows in the wall.
         """
         numRows = len(wall)
         columns = collections.defaultdict(lambda: numRows)
         
         for r in range(len(wall)):
             """
             col will be the current column that we are considering drawing a line through.
             If a brick in this row ends at column col, then we can avoid intersecting that
             brick by drawing a line through column col.
             """
             col = 0
             for c in range(len(wall[r])-1):
                 """
                 Add the length of the current brick to col to find the column where
                 this brick ends.
                 """
                 col += wall[r][c]
                 """
                 Because this brick ends at col, it won't be intersected if we draw a line
                 at column col. So we can decrement the number of bricks that will be intersected 
                 if we draw a line at column col.
                 """
                 columns[col] -= 1
         
         return numRows if len(columns) == 0 else min(columns.values())