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.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Data structures

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

Solutions:
class Solution:

	def leastBricks(self, wall):
		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):
		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]:
				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
		if x_to_edge_dict == {}:
			return rows
		else:
			return rows - max(x_to_edge_dict.values())


class Solution:

	def leastBricks(self, wall):
		if not wall:
			return 0
		hsh = collections.defaultdict(int)
		for row in wall:
			distance_from_left = 0
			for brick_width in row[:-1]:
				distance_from_left += brick_width
				hsh[distance_from_left] += 1
		return len(wall) - max(list(hsh.values()) + [0])


class Solution:

	def leastBricks(self, wall):
		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):
		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())