数据结构类型:栈
时间复杂度:O(N)文章来源:https://www.toymoban.com/news/detail-809648.html
空间复杂度:O(N)文章来源地址https://www.toymoban.com/news/detail-809648.html
代码实现:
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = []
area = 0
for i in range(len(heights)):
index = i
while stack and stack[-1][1] > heights[i]:
last_i, last_h = stack.pop()
index = last_i
area = max(area, last_h * (i - last_i))
stack.append([index, heights[i]])
for i, h in stack:
area = max(area, (len(heights) - i) * h)
return area
到了这里,关于(力扣记录)84. 柱状图中最大的矩形的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!