代码随想录算法训练营第六十天|84. 柱状图中最大的矩形
LeetCode 84. 柱状图中最大的矩形
题目链接:84. 柱状图中最大的矩形 - 力扣(LeetCode)
和接雨水还挺像的。
代码:
#python
class Solution:def largestRectangleArea(self, heights: List[int]) -> int:heights.insert(0, 0)heights.append(0)stack = [0]result = 0for i in range(1, len(heights)):while stack and heights[i] < heights[stack[-1]]:mid_height = heights[stack[-1]]stack.pop()if stack:# area = width * heightarea = (i - stack[-1] - 1) * mid_heightresult = max(area, result)stack.append(i)return result