其他分享
首页 > 其他分享> > 84. 柱状图中最大的矩形

84. 柱状图中最大的矩形

作者:互联网

https://leetcode-cn.com/problems/largest-rectangle-in-histogram/

方案:

https://leetcode-cn.com/problems/largest-rectangle-in-histogram/solution/84-by-ikaruga/

 

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        stacks = []
        max_value = 0
        heights = [0] + heights + [0]

        for i in range(len(heights)):
            while stacks and heights[stacks[-1]] > heights[i]:
                tmp_index = stacks.pop()
                tmp_value = heights[tmp_index]
                tmp_result = tmp_value * (i - stacks[-1] - 1)
                max_value = max(max_value, tmp_result)
            stacks.append(i)
        
        return max_value

 

标签:tmp,cn,max,value,heights,柱状图,矩形,stacks,84
来源: https://blog.csdn.net/wdh315172/article/details/112134909