MOD = 10 ** 9 + 7
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
n = len(arr)
monoStack = []
left = [0] * n
right = [0] * n
for i, x in enumerate(arr):
while monoStack and x <= arr[monoStack[-1]]:
monoStack.pop()
left[i] = i - (monoStack[-1] if monoStack else -1)
monoStack.append(i)
monoStack = []
for i in range(n - 1, -1, -1):
while monoStack and arr[i] < arr[monoStack[-1]]:
monoStack.pop()
right[i] = (monoStack[-1] if monoStack else n) - i
monoStack.append(i)
ans = 0
for l, r, x in zip(left, right, arr):
ans = (ans + l * r * x) % MOD
return ans
力扣10.28
最新推荐文章于 2024-09-09 18:27:53 发布