Thursday, September 18, 2014

Leetcode: Largest Rectangle in Histogram @Python

class Solution:
    # @param height, a list of integer
    # @return an integer
    def largestRectangleArea(self, height):
        height.insert(0,0)
        height.append(0)
        n=len(height)
        stack=[0]
        maxRec=0
        for i in range(1,n):
            while height[i]<height[stack[-1]]:
                k=stack.pop()
                newarea=(i-1-stack[-1])*height[k]
                if newarea>maxRec:
                    maxRec=newarea
            stack.append(i)
        return maxRec

No comments :

Post a Comment