84. 柱状图中最大的矩形
AC!
完结撒花。
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
res = 0
mono = [-1]
heights = heights + [-1]
for i in range(len(heights)):
while len(mono) > 1 and heights[mono[- 1]] > heights[i]:
ind = mono.pop()
res = max(res, heights[ind] * (i - mono[-1] - 1))
mono.append(i)
return res
今日总结:
能够完全理解单调栈了。
本文介绍了如何利用单调栈算法求解给定高度数组的柱状图中最大矩形面积的问题,通过动态维护单调性来找到每个位置构建的最大矩形。
979

被折叠的 条评论
为什么被折叠?



