Skip to content

Commit e627e69

Browse files
authored
Create Brick Wall.py
1 parent f5966ac commit e627e69

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Brick Wall.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
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.
3+
4+
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.
5+
6+
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.
7+
8+
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.
9+
10+
Example:
11+
12+
Input:
13+
[[1,2,2,1],
14+
[3,1,2],
15+
[1,3,2],
16+
[2,4],
17+
[3,1,2],
18+
[1,3,1,1]]
19+
Output: 2
20+
Explanation:
21+
22+
Note:
23+
24+
The width sum of bricks in different rows are the same and won't exceed INT_MAX.
25+
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.
26+
27+
'''
28+
29+
class Solution(object):
30+
def leastBricks(self, wall):
31+
"""
32+
:type wall: List[List[int]]
33+
:rtype: int
34+
"""
35+
gap_count = {}
36+
for row in wall:
37+
gap = 0
38+
for i in xrange(len(row) - 1):
39+
gap += row[i]
40+
if gap in gap_count:
41+
gap_count[gap] += 1
42+
else:
43+
gap_count[gap] = 1
44+
45+
max_count = 0
46+
for gap in gap_count:
47+
max_count = max(max_count, gap_count[gap])
48+
49+
return len(wall) - max_count

0 commit comments

Comments
 (0)