Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def largestRectangleArea(heights):
- leftSmall = []
- rightSmall = [-1]*len(heights)
- stack = []
- for i in range(len(heights)):
- while(stack and heights[i] <= stack[-1][1]):
- stack.pop()
- if(stack):
- leftSmall.append(stack[-1][0]+1)
- else:
- leftSmall.append(0)
- stack.append([i,heights[i]])
- stack = []
- for i in range(len(heights)-1, -1, -1):
- while(stack and heights[i] <= stack[-1][1]):
- stack.pop()
- if(stack):
- rightSmall[i] = stack[-1][0]-1
- else:
- rightSmall[i] = len(heights)-1
- stack.append([i, heights[i]])
- res = 0
- for i in range(len(heights)):
- val = (rightSmall[i]-leftSmall[i]+1)*heights[i]
- res = max(res, val)
- return res
- heights = [2, 1, 5, 6, 2, 3, 1]
- print(largestRectangleArea(heights))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement