Python


  • 0
    D
    class Solution(object):
        def largestRectangleArea(self, heights):
            """
            :type heights: List[int]
            :rtype: int
            """
            heights += [0]
            stack = []
            area = 0
            for i, val in enumerate(heights):
                while stack and heights[stack[-1]] > val:
                    rect_height = heights[stack.pop()]
                    begidx = stack[-1] if stack else -1
                    area = max(area, (i - begidx - 1) * rect_height)
                stack.append(i)
            return area

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.