-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlargest.py
27 lines (22 loc) · 862 Bytes
/
largest.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
from typing import List
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = [-1]
max_area = 0
for i in range(len(heights)):
while stack[-1] != -1 and heights[stack[-1]] >= heights[i]:
current_height = heights[stack.pop()]
current_width = i - stack[-1] - 1
max_area = max(max_area, current_height * current_width)
stack.append(i)
while stack[-1] != -1:
current_height = heights[stack.pop()]
current_width = len(heights) - stack[-1] - 1
max_area = max(max_area, current_height * current_width)
return max_area
# Tests:
if __name__ == '__main__':
Instant = Solution()
Solve = Instant.largestRectangleArea([2,1,5,6,2,3])
# [2,1,5,6,2,3] -> 10
print(Solve)