Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a969646

Browse files
Create 907.py
1 parent 227b80f commit a969646

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

501-1000/907.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def sumSubarrayMins(self, arr: List[int]) -> int:
3+
ans = 0
4+
stack = []
5+
arr = [float('-inf')] + arr + [float('-inf')]
6+
for i, n in enumerate(arr):
7+
while stack and arr[stack[-1]] > n:
8+
cur = stack.pop()
9+
ans += arr[cur] * (i - cur) * (cur - stack[-1])
10+
stack.append(i)
11+
return ans % (10**9 + 7)
12+

0 commit comments

Comments
 (0)