LC084 - Largest Rectangle in Histogram
Problem
Example
Solution
def largestRectangleArea(self, heights: List[int]) -> int:
maxArea = 0
stack = []
for index, height in enumerate(heights):
begin = index
while begin and stack[-1][1] > height:
i, h = stack.pop()
maxArea = max(maxArea, (index-i) * h)
begin = i
stack.append((begin, height))
for tup in stack:
index, height = tup[0], tup[1]
maxArea = max(maxArea, (len(heights)-index)*height)
return maxArea
Last updated