Python solution using stack 66ms

  • 0
    class Solution(object):
        def largestRectangleArea(self, heights):
            stack = [(0,-1)]
            maxArea = -1
            for i,h in enumerate(heights):
                index0 = i
                while stack and stack[-1][0] > h:
                    h0, index0 = stack.pop()
                    area0 = h0*(i-index0)
                    if area0 > maxArea:
                        maxArea = area0
                stack.append((h, index0))
            return maxArea

Log in to reply

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