-
Notifications
You must be signed in to change notification settings - Fork 14
/
84-fivestar1103.py
24 lines (23 loc) · 1019 Bytes
/
84-fivestar1103.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
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
from collections import deque
heights, myStack, left = [0] + heights, [[0, 0]], []
for i in range(1, len(heights)):
while True:
if not myStack or myStack[-1][1] < heights[i]:
left.append(myStack[-1][0] if myStack else 0)
myStack.append([i, heights[i]])
break
myStack.pop()
myStack, area, ans = [[len(heights), 0]], deque(), -1
for i in range(len(heights) - 1, 0, -1):
while True:
if not myStack or myStack[-1][1] < heights[i]:
tmp = ((myStack[-1][0] if myStack else len(heights)) - left[i - 1] - 1) * heights[i]
if ans < tmp:
ans = tmp
area.appendleft(tmp)
myStack.append([i, heights[i]])
break
myStack.pop()
return ans