Advertisement
serega1112

84

Dec 24th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. class Solution:
  2.     def largestRectangleArea(self, heights: List[int]) -> int:
  3.         n = len(heights)
  4.         prev_lt = [i for i in range(n)]
  5.         st_inc_prev = []
  6.         next_lt = [n - i - 1 for i in range(n)]
  7.         st_inc_next = []
  8.        
  9.         for i, h in enumerate(heights):
  10.             while st_inc_prev and h <= heights[st_inc_prev[-1]]:
  11.                 st_inc_prev.pop()
  12.             if st_inc_prev:
  13.                 prev_lt[i] = i - st_inc_prev[-1] - 1
  14.             st_inc_prev.append(i)
  15.            
  16.             while st_inc_next and h < heights[st_inc_next[-1]]:
  17.                 x = st_inc_next.pop()
  18.                 next_lt[x] = i - x - 1
  19.             st_inc_next.append(i)
  20.            
  21.         res = 0
  22.        
  23.         for i in range(len(heights)):
  24.             res = max(res, (1 + prev_lt[i] + next_lt[i]) * heights[i])
  25.            
  26.         return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement