-
Notifications
You must be signed in to change notification settings - Fork 1
/
largest-rectangle-in-histogram.py
37 lines (26 loc) · 1.18 KB
/
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
# Leetcode 84. Largest Rectangle in Histogram
#
# Link: https://leetcode.com/problems/largest-rectangle-in-histogram/
# Difficulty: Hard
# Solution using stack and extend logic
# Complexity:
# O(N) time | where N is the length of the input list
# O(N) space | where N is the length of the input list
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = [] # pair: index, height
max_area = 0
for i, h in enumerate(heights):
start = i #
while stack and stack[-1][1] > h:
# we remove old heigher element since they cannot extend further
higher_index, higher_height = stack.pop()
max_area = max(max_area, higher_height * (i - higher_index))
# we also extend our start index since was heigher
start = higher_index
# we add the current height with the new start, since we can extend on left side
stack.append((start, h))
# All remaining heights, can be extended left through all the graph
for i, h in stack:
max_area = max(max_area, h * (len(heights) - i))
return max_area