-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path84. Largest Rectangle in Histogram.py
45 lines (42 loc) · 1.23 KB
/
84. Largest Rectangle in Histogram.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
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
if not heights:
return 0
n = len(heights)
left = [0] * n
right = [0] * n
left[0] = -1
right[-1] = n
for i in range(1, n):
tmp = i - 1
while tmp >= 0 and heights[tmp] >= heights[i]:
tmp = left[tmp]
left[i] = tmp
for j in range(n - 2, -1, -1):
tmp = j + 1
while tmp < n and heights[tmp] >= heights[j]:
tmp = right[tmp]
right[j] = tmp
ans = 0
for i in range(n):
ans = max(ans, (right[i] - left[i] - 1) * heights[i])
return ans
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
heights = [0] + heights + [0]
stack = []
ans = 0
for i in range(len(heights)):
while stack and heights[stack[-1]] > heights[i]:
tmp = stack.pop()
ans = max(ans, (i - stack[-1] - 1) * heights[tmp])
stack.append(i)
return ans