Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sum Subarray Minimums #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions 0907-Sum-of-Subarray-Minimums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarray of A.

Since the answer may be large, return the answer modulo 10^9 + 7.



Example 1:

Input: [3,1,2,4]
Output: 17
Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Sum is 17.



Note:

1 <= A.length <= 30000
1 <= A[i] <= 30000
'''
# Time Complexity: O(n)
# Space Complexity: O(n)
class Solution:
def sumSubarrayMins(self, A: List[int]) -> int:
res = 0
s = []
A = [0] + A + [0]
for idx, a in enumerate(A):
while s and A[s[-1]] > a:
i = s.pop()
j = s[-1]
res += A[i] * (idx - i) * (i - j)
s.append(idx)
return res % (10 ** 9 + 7)