serega1112

907

Dec 24th, 2020 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. class Solution:
  2.     def sumSubarrayMins(self, arr: List[int]) -> int:
  3.         n = len(arr)
  4.         next_lte = [n - i for i in range(n)]
  5.         prev_lt = [i + 1 for i in range(n)]
  6.         st_inc = []
  7.        
  8.         for i, num in enumerate(arr):
  9.             while st_inc and num < arr[st_inc[-1]]:
  10.                 x = st_inc.pop()
  11.                 next_lte[x] = i - x  
  12.             if st_inc:
  13.                 prev_lt[i] = i - st_inc[-1]  
  14.             st_inc.append(i)
  15.            
  16.         res = 0
  17.        
  18.         for i in range(len(arr)):
  19.             res += next_lte[i] * prev_lt[i] * arr[i]
  20.            
  21.         return res % (10**9 + 7)
Add Comment
Please, Sign In to add comment