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
版权声明:本文为weixin_45405128原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。