-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0084-largest-rectangle-in-histogram.py
More file actions
28 lines (23 loc) · 931 Bytes
/
0084-largest-rectangle-in-histogram.py
File metadata and controls
28 lines (23 loc) · 931 Bytes
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
# time complexity: O(n)
# space complexity: O(n)
from typing import List
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
monoStack = []
maxArea = 0
for i in range(len(heights)):
while monoStack and heights[monoStack[-1]] >= heights[i]:
currHeight = heights[monoStack.pop()]
currWidth = i if not monoStack else i - monoStack[-1] - 1
maxArea = max(maxArea, currHeight * currWidth)
monoStack.append(i)
n = len(heights)
while monoStack:
currHeight = heights[monoStack.pop()]
currWidth = n if not monoStack else n - monoStack[-1] - 1
maxArea = max(maxArea, currHeight * currWidth)
return maxArea
heights = [2, 1, 5, 6, 2, 3]
print(Solution().largestRectangleArea(heights))
heights = [2,4]
print(Solution().largestRectangleArea(heights))