Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def sumSubarrayMins(self, arr: List[int]) -> int:
- n = len(arr)
- next_lte = [n - i for i in range(n)]
- prev_lt = [i + 1 for i in range(n)]
- st_inc = []
- for i, num in enumerate(arr):
- while st_inc and num < arr[st_inc[-1]]:
- x = st_inc.pop()
- next_lte[x] = i - x
- if st_inc:
- prev_lt[i] = i - st_inc[-1]
- st_inc.append(i)
- res = 0
- for i in range(len(arr)):
- res += next_lte[i] * prev_lt[i] * arr[i]
- return res % (10**9 + 7)
Add Comment
Please, Sign In to add comment