84. 柱状图中最大的矩形
class LargestRectangleArea:"""时间复杂度:O(N)空间复杂度:O(N)84. 柱状图中最大的矩形https://leetcode.cn/problems/largest-rectangle-in-histogram/description/"""def solution(self, heights: List[int]) -> int:n = len(heights)if n == 0:return 0# left[i]和right[i] 分别表示 第i个元素的左右两侧的最近的高度小于heights[i]的柱子编号left, right = [0] * n, [n] * n# 单调递增栈mono_stack = []for i in range(n):while mono_stack and heights[mono_stack[-1]] >= heights[i]:right[mono_stack[-1]] = imono_stack.pop()left[i] = mono_stack[-1] if mono_stack else -1mono_stack.append(i)ans = max((right[i]-left[i]-1) * heights[i] for i in range(n))return ans