907. Sum of Subarray Minimums - Detailed Explanation
Problem Statement
Description:
Given an array of integers arr, find the sum of the minimum value of every (contiguous) subarray of arr. Since the answer can be very large, return it modulo (10^9 + 7).
Examples:
- 
Example 1: - Input: arr = [3,1,2,4]
- Output: 17
- Explanation:
 The subarrays of[3,1,2,4]and their minimums are:- [3]→ 3
- [3,1]→ 1
- [3,1,2]→ 1
- [3,1,2,4]→ 1
- [1]→ 1
- [1,2]→ 1
- [1,2,4]→ 1
- [2]→ 2
- [2,4]→ 2
- [4]→ 4
 Sum = 3 + 1 + 1 + 1 + 1 + 1 + 1 + 2 + 2 + 4 = 17
 
 
- Input: 
- 
Example 2: - Input: arr = [11,81,94,43,3]
- Output: 444
- Explanation:
 All possible subarrays contribute to the final sum based on their minimum values.
 
- Input: 
Constraints:
- (1 \leq \text{arr.length} \leq 3 \times 10^4)
- (1 \leq \text{arr}[i] \leq 3 \times 10^4)
Hints Before the Solution
- 
Brute Force Idea: 
 You could generate every subarray and compute its minimum, but this approach has an O(n²) or worse time complexity and will be too slow for larger inputs.
- 
Observation for Optimality: 
 Instead of checking every subarray, think about the contribution of each element inarrto the final sum. If you can count how many subarrays havearr[i]as the minimum, you can multiplyarr[i]by that count and add it to the sum.
- 
Monotonic Stack: 
 A monotonic (increasing) stack is a useful tool to determine, for each element, the span of subarrays for which it is the minimum. In particular, calculate:- Left Span: How many contiguous subarrays ending at i(extending to the left) wherearr[i]is the smallest.
- Right Span: How many contiguous subarrays starting at i(extending to the right) wherearr[i]remains the smallest.
 
- Left Span: How many contiguous subarrays ending at 
Approaches
Approach 1: Brute Force (For Understanding Only)
Idea:
- Iterate over all possible subarrays.
- For each subarray, compute the minimum element.
- Sum all the minimums.
Issues:
- The time complexity is O(n²) (or worse) which is impractical given the constraints.
Approach 2: Optimal Monotonic Stack Method
Idea:
For each element arr[i], determine the number of subarrays in which it is the minimum by using the following two concepts:
- 
Previous Less Element: 
 Find the distance fromiback to the previous index where the element is strictly less thanarr[i]. Let this distance beleft[i](if there is no such element, usei + 1).
- 
Next Less Element: 
 Find the distance fromiforward to the next index where the element is less than or equal toarr[i](to handle duplicate values correctly). Let this distance beright[i](if there is no such element, usen - i).
Contribution of arr[i]:
Each subarray in which arr[i] is the minimum can be formed by choosing any of the left[i] choices on the left and any of the right[i] choices on the right. Therefore, the total contribution of arr[i] is:
[
\text{contribution} = \text{arr}[i] \times \text{left}[i] \times \text{right}[i]
]
Why It Works:
- By summing up the contributions from all indices, you count the minimum for every subarray exactly once.
- Using a monotonic stack, you can compute the spans in O(n) time.
Code Implementations
Python Implementation
Java Implementation
Complexity Analysis
- 
Time Complexity: 
 Both the left and right span computations are done in O(n) time using a monotonic stack. The final summing step is also O(n). Thus, the overall time complexity is O(n).
- 
Space Complexity: 
 O(n) space is required for the left/right arrays and the stack.
Step-by-Step Walkthrough with Visual Example
Consider arr = [3, 1, 2, 4]:
- 
Left Calculation: - For index 0 (value 3):
 No previous elements → left[0] = 1.
- For index 1 (value 1):
 3 (at index 0) > 1, so left[1] = 1 (from itself) + left[0] = 2.
- For index 2 (value 2):
 1 (at index 1) is not greater than 2 → left[2] = 1.
- For index 3 (value 4):
 2 (at index 2) is less than 4 → left[3] = 1.
 
- For index 0 (value 3):
- 
Right Calculation (scanning from right): - For index 3 (value 4):
 No elements to the right → right[3] = 1.
- For index 2 (value 2):
 4 (at index 3) is greater than 2 → right[2] = 1 + right[3] = 2.
- For index 1 (value 1):
 Both 2 and 4 to the right are greater than 1 → right[1] = 1 + right[2] + ... = 3.
- For index 0 (value 3):
 1 (at index 1) is less than 3 → right[0] = 1.
 
- For index 3 (value 4):
- 
Contribution Calculation: - Index 0: (3 \times 1 \times 1 = 3)
- Index 1: (1 \times 2 \times 3 = 6)
- Index 2: (2 \times 1 \times 2 = 4)
- Index 3: (4 \times 1 \times 1 = 4)
- Sum = 3 + 6 + 4 + 4 = 17
 
Common Mistakes
- 
Incorrect Stack Conditions: 
 Handling duplicates requires careful use of strict versus non-strict inequalities. For left spans, use>; for right spans, use>=.
- 
Off-by-One Errors: 
 Ensure that when no previous or next smaller element exists, you correctly use the index offsets (i.e.,i + 1orn - i).
- 
Modulus Handling: 
 Since the result can be very large, remember to take the modulo (10^9 + 7) at the appropriate steps.
Edge Cases
- 
Single Element Array: 
 The only subarray is the element itself.
- 
All Elements Equal: 
 Each subarray’s minimum is the same as any element. The stack logic must correctly count overlapping spans.
- 
Strictly Increasing or Decreasing Arrays: 
 The left/right arrays will be at their maximum or minimum possible values respectively.
Alternative Variations and Related Problems
- 
Alternative Variation: 
 You might be asked for the sum of subarray maximums. A similar approach with slight adjustments (reversing the inequality) can be used.
- 
Related Problems for Further Practice: - Largest Rectangle in Histogram
- Subarray Sum Problems
- Range Minimum Query
 
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78