-
Notifications
You must be signed in to change notification settings - Fork 43
/
brick-wall.py
164 lines (137 loc) · 4.73 KB
/
brick-wall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
554. Brick Wall
Medium
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.
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
Constraints:
n == wall.length
1 <= n <= 104
1 <= wall[i].length <= 104
1 <= sum(wall[i].length) <= 2 * 104
sum(wall[i]) is the same for each row i.
1 <= wall[i][j] <= 231 - 1
"""
# V0
# IDEA : HASH TABLE + COUNTER UPDATE (looping every element in the list and cumsum and get the max count)
### DEMO : counter.update()
# In [87]: _counter = collections.Counter()
# In [88]: _counter
# Out[88]: Counter()
# In [89]: _counter.update([1])
# In [90]: _counter
# Out[90]: Counter({1: 1})
# In [91]: _counter.update([1])
# In [92]: _counter
# Out[92]: Counter({1: 2})
# In [93]: _counter.update([2])
# In [94]: _counter
# Out[94]: Counter({1: 2, 2: 1})
import collections
class Solution(object):
def leastBricks(self, wall):
_counter = collections.Counter()
count = 0
# go through every sub-wall in wall
for w in wall:
cum_sum = 0
# go through every element in sub-wall
for i in range(len(w) - 1):
cum_sum += w[i]
### NOTE we can update collections.Counter() via below
_counter.update([cum_sum])
count = max(count, _counter[cum_sum])
return len(wall) - count
# V0'
# IDEA : HASH TABLE + COUNTER UPDATE (looping every element in the list and cumsum and get the max count)
class Solution(object):
def leastBricks(self, wall):
left_counter = collections.Counter()
count = 0
for row in wall:
left = 0
for i in range(len(row) - 1):
left += row[i]
left_counter.update([left])
count = max(count, left_counter[left])
return len(wall) - count
# V1
# http://bookshadow.com/weblog/2017/04/09/leetcode-brick-wall/
class Solution(object):
def leastBricks(self, wall):
"""
:type wall: List[List[int]]
:rtype: int
"""
rims = collections.Counter()
for bricks in wall:
cnt = 0
for b in bricks:
if cnt: rims[cnt] += 1
cnt += b
return len(wall) - max(rims.values() or [0])
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/80526298
class Solution(object):
def leastBricks(self, wall):
"""
:type wall: List[List[int]]
:rtype: int
"""
left_counter = collections.Counter()
count = 0
for row in wall:
left = 0
for i in range(len(row) - 1):
left += row[i]
left_counter.update([left])
count = max(count, left_counter[left])
return len(wall) - count
# V1''
# IDEA : HASHMAP
# https://leetcode.com/problems/brick-wall/solution/
# JAVA
# public class Solution {
# public int leastBricks(List < List < Integer >> wall) {
# HashMap < Integer, Integer > map = new HashMap < > ();
# for (List < Integer > row: wall) {
# int sum = 0;
# for (int i = 0; i < row.size() - 1; i++) {
# sum += row.get(i);
# if (map.containsKey(sum))
# map.put(sum, map.get(sum) + 1);
# else
# map.put(sum, 1);
# }
# }
# int res = wall.size();
# for (int key: map.keySet())
# res = Math.min(res, wall.size() - map.get(key));
# return res;
# }
# }
# V2
# Time: O(n), n is the total number of the bricks
# Space: O(m), m is the total number different widths
import collections
class Solution(object):
def leastBricks(self, wall):
"""
:type wall: List[List[int]]
:rtype: int
"""
widths = collections.defaultdict(int)
result = len(wall)
for row in wall:
width = 0
for i in range(len(row)-1):
width += row[i]
widths[width] += 1
result = min(result, len(wall) - widths[width])
return result