-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path85.py
36 lines (33 loc) · 969 Bytes
/
85.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
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix or not matrix[0]:
return 0
ret = 0
col = len(matrix[0])
height = [0 for _ in range(col+1)]
for i in range(len(matrix)):
for j in range(col):
if matrix[i][j]=='0':
height[j]=0
else:
height[j]+=1
stack = [-1]
for k in range(col+1):
while height[stack[-1]]>height[k]:
h = height[stack.pop()]
w = k-stack[-1]-1
ret = max(ret, h*w)
stack.append(k)
return ret
if __name__ == '__main__':
solution = Solution()
print solution.maximalRectangle([
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
])