Review Note
Last Update: 07/03/2024 02:06 AM
Current Deck: LeetCode
Published
Fields:
Front
84. Largest Rectangle in Histogram
- Given an array of integers
heights
representing the histogram's bar height where the width of each bar is1
, return the area of the largest rectangle in the histogram.
Back
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
maxArea = 0
stack = [] # (i, height)
heights.append(0)
for i, height in enumerate(heights):
start = i
while stack and height < stack[-1][1]:
iPrev, heightPrev = stack.pop()
maxArea = max(maxArea, heightPrev * (i - iPrev))
start = iPrev
stack.append((start, height))
return maxArea
Tags:
Suggested Changes:
Deck Changes (Suggestion to move the Note to the following Deck):
Field Changes:
Tag Changes: