题目不难,但是涉及到的知识点很丰富。
class Solution: def sumSubarrayMins(self, arr: List[int]) -> int: MOD = 10 ** 9 + 7 n = len(arr) pre = [-1] * n suf = [n] * n stk = [] for i in range(n): while stk and arr[stk[-1]] >= arr[i]: stk.pop() if stk: pre[i] = stk[-1] stk.append(i) stk.clear() for i in range(n - 1, -1, -1): while stk and arr[stk[-1]] > arr[i]: stk.pop() if stk: suf[i] = stk[-1] stk.append(i) return sum((i - pre[i]) * (suf[i] - i) * v for i, v in enumerate(arr)) % MOD
标签:pre,suf,arr,907,后缀,pop,stk,int,最小值 From: https://www.cnblogs.com/zk6696/p/17859841.html