Largest Rectangle in Histogram LeetCode solution in Python
1class Solution:2def largestRectangleArea(self, heights: List[int]) -> int:3maxArea = 04stack = [] # (index, height)56for i, h in enumerate(heights):7startIndex = i8while stack and stack[-1][1] > h:9index, height = stack.pop()10maxArea = max(maxArea, height * (i - index))11startIndex = index12stack.append((startIndex, h))1314for index, height in stack:15maxArea = max(maxArea, height * (len(heights) - index))1617return maxArea