Sum of Subarray Minimums

Given an integer array arr, return the sum of the minimum value of every subarray. Since the answer can be large, return it modulo 1,000,000,007. Example: Input: arr = [3,1,2,4] Output: 17 Explanation: The minimum of each subarray contributes to the final sum. Pattern focus: Range Contribution using monotonic stacks.

Input Format

arr = integer array

Output Format

sum of minimums across all subarrays, modulo 1,000,000,007

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^9

Examples

Example 1:

Input:

arr = [3,1,2,4]

Output:

17

Explanation:

Count how many subarrays each element becomes the minimum for.

Example 2:

Input:

arr = [11,81,94,43,3]

Output:

444

Explanation:

Contribution counting avoids enumerating all subarrays.

Loading...
Sum of Subarray Minimums - Monotonic Stack Queue