Skip to content

Commit 6ca5d1c

Browse files
authored
Update maximal-rectangle.py
1 parent 6e5010f commit 6ca5d1c

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

Python/maximal-rectangle.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,21 @@ def maximalRectangle(self, matrix):
88
:rtype: int
99
"""
1010
def largestRectangleArea(heights):
11-
increasing, area, i = [], 0, 0
12-
while i <= len(heights):
13-
if not increasing or (i < len(heights) and heights[i] > heights[increasing[-1]]):
14-
increasing.append(i)
15-
i += 1
16-
else:
17-
last = increasing.pop()
18-
if not increasing:
19-
area = max(area, heights[last] * i)
20-
else:
21-
area = max(area, heights[last] * (i - increasing[-1] - 1 ))
22-
return area
11+
stk, result, i = [-1], 0, 0
12+
for i in xrange(len(heights)+1):
13+
while stk[-1] != -1 and (i == len(heights) or heights[stk[-1]] >= heights[i]):
14+
result = max(result, heights[stk.pop()]*((i-1)-stk[-1]))
15+
stk.append(i)
16+
return result
2317

2418
if not matrix:
2519
return 0
26-
2720
result = 0
28-
heights = [0] * len(matrix[0])
21+
heights = [0]*len(matrix[0])
2922
for i in xrange(len(matrix)):
3023
for j in xrange(len(matrix[0])):
3124
heights[j] = heights[j] + 1 if matrix[i][j] == '1' else 0
3225
result = max(result, largestRectangleArea(heights))
33-
3426
return result
3527

3628

0 commit comments

Comments
 (0)