Skip to content

Commit 28246ef

Browse files
authored
Update largest-rectangle-in-histogram.py
1 parent 2d3661d commit 28246ef

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

Python/largest-rectangle-in-histogram.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
# Space: O(n)
33

44
class Solution(object):
5-
# @param height, a list of integer
6-
# @return an integer
7-
def largestRectangleArea(self, height):
8-
increasing, area, i = [], 0, 0
9-
while i <= len(height):
10-
if not increasing or (i < len(height) and height[i] > height[increasing[-1]]):
11-
increasing.append(i)
12-
i += 1
13-
else:
14-
last = increasing.pop()
15-
if not increasing:
16-
area = max(area, height[last] * i)
17-
else:
18-
area = max(area, height[last] * (i - increasing[-1] - 1 ))
19-
return area
20-
5+
def largestRectangleArea(self, heights):
6+
"""
7+
:type heights: List[int]
8+
:rtype: int
9+
"""
10+
stk, result, i = [-1], 0, 0
11+
for i in xrange(len(heights)+1):
12+
while stk[-1] != -1 and (i == len(heights) or heights[stk[-1]] >= heights[i]):
13+
result = max(result, heights[stk.pop()]*(i-stk[-1]-1))
14+
stk.append(i)
15+
return result

0 commit comments

Comments
 (0)