clean python, O(n), stack


  • 0
    S
    class Solution(object):
        def largestRectangleArea(self, heights):
            heights.append(0)
            stackk = []
            res = 0
            for i in range(len(heights)):
                index = i
                while stackk and stackk[-1][1] > heights[i]:
                    (index,height) = stackk.pop()
                    res = max(res,height * (i-index))
                stackk.append((index,heights[i]))
            return res
    

Log in to reply
 

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