907. Sum of Subarray Minimums - Explanation
Description
For every nonempty contiguous subarray of arr, take its minimum value. Return the sum of those minimums, modulo 1000000007.
Example 1:
Input: arr = [2,1,2]
Output: 8Explanation: The three length-1 subarrays contribute 2 + 1 + 2 = 5. The two length-2 subarrays each contribute 1, and the whole array contributes 1, for a total of 8.
Example 2:
Input: arr = [5]
Output: 5Explanation: The only nonempty subarray is [5], whose minimum is 5.
Example 3:
Input: arr = [4,4,4]
Output: 24Explanation: There are 3 + 2 + 1 = 6 nonempty subarrays. Each has minimum 4, so the sum is 6 * 4 = 24.
Constraints:
1 <= arr.length <= 300001 <= arr[i] <= 30000
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Monotonic Stack - Used to efficiently find next/previous smaller elements for each position in O(n) time
- Contribution Technique - Understanding how to count each element's contribution across all subarrays rather than iterating through each subarray
- Modular Arithmetic - Applying modulo operations correctly to prevent integer overflow in the result
1. Brute Force
Intuition
For each subarray, we need to find its minimum element and add it to the total sum. The straightforward approach is to enumerate all possible subarrays by their start and end positions, tracking the running minimum as we extend each subarray.
Algorithm
- Initialize
res = 0to store the sum of minimums. - For each starting index
i:- Initialize
minVal = arr[i]. - For each ending index
jfromiton - 1:- Update
minVal = min(minVal, arr[j]). - Add
minValtores(modulo 10^9 + 7).
- Update
- Initialize
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Monotonically Increasing Stack (Two Pass)
Intuition
Instead of computing minimums for each subarray, we can ask: for each element, how many subarrays is it the minimum of? An element arr[i] is the minimum of all subarrays that start after the previous smaller element and end before the next smaller element. A monotonically increasing stack helps us efficiently find these boundaries.
Algorithm
- Use a stack to find
prevSmaller[i]: the index of the previous smaller element (or -1 if none). - Use another stack pass to find
nextSmaller[i]: the index of the next smaller or equal element (ornif none). We use "smaller or equal" on one side to avoid double counting. - For each element at index
i:left = i - prevSmaller[i]is the count of valid starting positions.right = nextSmaller[i] - iis the count of valid ending positions.- Add
arr[i] * left * rightto the result.
- Return
resmodulo 10^9 + 7.
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
MOD = 10**9 + 7
n = len(arr)
# Compute previous smaller
prev_smaller = [-1] * n
stack = []
for i in range(n):
while stack and arr[stack[-1]] > arr[i]:
stack.pop()
prev_smaller[i] = stack[-1] if stack else -1
stack.append(i)
# Compute next smaller
next_smaller = [n] * n
stack = []
for i in range(n - 1, -1, -1):
while stack and arr[stack[-1]] >= arr[i]:
stack.pop()
next_smaller[i] = stack[-1] if stack else n
stack.append(i)
res = 0
for i in range(n):
left = i - prev_smaller[i]
right = next_smaller[i] - i
res = (res + arr[i] * left * right) % MOD
return resTime & Space Complexity
- Time complexity:
- Space complexity:
3. Monotonically Increasing Stack (One Pass)
Intuition
We can combine finding the previous smaller and next smaller into a single pass. By adding sentinel values (negative infinity) at both ends of the array, we ensure every element gets processed. When we pop an element from the stack upon finding a smaller value, we immediately know both its left boundary (from the new stack top) and right boundary (the current index).
Algorithm
- Pad the array with negative infinity at both ends.
- Maintain a stack of
(index, value)pairs. - For each element in the padded array:
- While the stack is not empty and the current element is smaller than the stack top:
- Pop the top element (index
j, valuem). left = j - stack_top_index(orj + 1if stack is empty).right = i - j.- Add
m * left * rightto the result.
- Pop the top element (index
- Push the current
(index, value)onto the stack.
- While the stack is not empty and the current element is smaller than the stack top:
- Return
resmodulo 10^9 + 7.
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
MOD = 10 ** 9 + 7
res = 0
arr = [float("-inf")] + arr + [float("-inf")]
stack = [] # (index, num)
for i, n in enumerate(arr):
while stack and n < stack[-1][1]:
j, m = stack.pop()
left = j - stack[-1][0] if stack else j + 1
right = i - j
res = (res + m * left * right) % MOD
stack.append((i, n))
return resTime & Space Complexity
- Time complexity:
- Space complexity:
4. Monotonically Increasing Stack (Optimal)
Intuition
We can eliminate the sentinel values by handling the edge cases explicitly. Instead of padding the array, we iterate one position past the end and use a conditional check to treat the out-of-bounds position as having a smaller value than any element. This triggers final processing of remaining stack elements.
Algorithm
- Initialize an empty stack and
res = 0. - Iterate from
i = 0ton(inclusive):- While the stack is not empty and (we're at the end OR
arr[i] < arr[stack_top]):- Pop index
jfrom the stack. left = j - stack_top(orj + 1if stack is empty).right = i - j.- Add
arr[j] * left * righttores.
- Pop index
- Push
ionto the stack.
- While the stack is not empty and (we're at the end OR
- Return
resmodulo 10^9 + 7.
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
MOD = 10**9 + 7
stack = []
res, n = 0, len(arr)
for i in range(n + 1):
while stack and (i == n or arr[i] < arr[stack[-1]]):
j = stack.pop()
left = j - (stack[-1] if stack else -1)
right = i - j
res = (res + arr[j] * left * right) % MOD
stack.append(i)
return resTime & Space Complexity
- Time complexity:
- Space complexity:
5. Dynamic Programming + Stack
Intuition
We can think of this problem dynamically. Let dp[i] represent the sum of minimums of all subarrays ending at index i. When we add element arr[i], we need to consider: for subarrays where arr[i] is not the minimum, we can reuse previously computed values; for subarrays where arr[i] is the minimum, we count how many such subarrays exist. The stack helps us find where arr[i] stops being the minimum.
Algorithm
- Initialize
dparray and an empty stack. - For each index
i:- Pop elements from the stack while
arr[stack_top] > arr[i]. - Let
jbe the stack top after popping (or -1 if empty). This is the index of the previous smaller or equal element. dp[i] = dp[j] + arr[i] * (i - j):dp[j]accounts for subarrays ending beforejwherearr[i]is not the minimum.arr[i] * (i - j)accounts for subarrays from positionsj+1toiwherearr[i]is the minimum.
- Add
dp[i]toresand pushionto the stack.
- Pop elements from the stack while
- Return
resmodulo 10^9 + 7.
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
MOD = 10**9 + 7
n = len(arr)
dp = [0] * n
stack, res = [], 0
for i in range(n):
while stack and arr[stack[-1]] > arr[i]:
stack.pop()
j = stack[-1] if stack else -1
dp[i] = (dp[j] if j != -1 else 0) + arr[i] * (i - j)
dp[i] %= MOD
res = (res + dp[i]) % MOD
stack.append(i)
return resTime & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Double Counting with Duplicate Elements
When there are duplicate values in the array, using strict less-than (<) on both sides when finding previous and next smaller elements causes the same subarray to be counted multiple times. The solution is to use strict less-than (<) on one side and less-than-or-equal (<=) on the other side to ensure each subarray is counted exactly once.
Integer Overflow in Contribution Calculation
The contribution formula arr[i] * left * right can overflow a 32-bit integer when the array is large. With n up to 30,000 and values up to 30,000, the product can reach approximately 2.7 * 10^13. Always use 64-bit integers for the multiplication and apply the modulo operation at each step.
Incorrect Boundary Initialization
When no previous smaller element exists, prevSmaller[i] should be -1 (not 0), representing an imaginary boundary before the array. Similarly, when no next smaller element exists, nextSmaller[i] should be n (not n-1). Using wrong boundary values leads to incorrect counts for subarrays starting at the beginning or ending at the end of the array.
Forgetting to Apply Modulo Operation
The problem requires returning the result modulo 10^9 + 7. A common mistake is only applying modulo at the final return statement. Since intermediate sums can overflow even with 64-bit integers after many additions, modulo should be applied after each addition to the result to keep values within bounds.
Sign in to join the discussion