Leetcode 84 largest Rectangle Area

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        stack = [(0, -1)]#dummy variable, (0.-1) o represens dummy initial height, -1 represents dummy intial position
        max_area=0
        for idx,h in enumerate(heights):
            while stack[-1][1] !=-1 and stack[-1][0]>=h:
                prev_pop=stack.pop()
                #print('prev_pop:{}'.format(prev_pop))
                #print('idx:{}'.format(idx))
                #print('stack[-1][1]:{}'.format(stack[-1][1]))
                max_area=max(max_area,prev_pop[0]*(idx-stack[-1][1]-1))
                #print(max_area)
            stack.append((h,idx))
            #print(stack)
        while stack[-1][1]!=-1:
            prev_pop=stack.pop()
            max_area =max(max_area,prev_pop[0]*(len(heights)-stack[-1][1]-1))
            #print(max_area)
        return max_area

 

你可能感兴趣的:(Python,编程笔试,Leetcode)