Largest Rectangle

  • + 0 comments

    def largestRectangle(h): # Write your code here stack = [0] area = 0 top = 0 h.append(0) for i in range(1, len(h)):

        if h[i] >= h[stack[-1]]:
            stack.append(i)
    
        else:
            while stack and h[i] < h[stack[-1]]:
                last = stack.pop()
                if stack:
                    area = (i - stack[-1]-1) * h[last]
                else:
                    area = i * h[last]
                if  area > top:
                    top = area
            stack.append(i)
    
    return top