Sum of Subarray Minimums
Given a nonempty positive integer array nums, return the sum of the minimum value of every contiguous nonempty subarray, modulo 1000000007. Subarrays are…
Given a nonempty positive integer array `nums`, return the sum of the minimum value of every contiguous nonempty subarray, modulo `1000000007`. Subarrays are distinguished by their positions. Equal values can belong to several subarrays, but each subarray contributes exactly one minimum.
- `1 <= nums.length <= 100000` - `1 <= nums[i] <= 1000000` - Return the sum modulo `1000000007`; do not change the input array.
Example 1: input nums = [3,1,2,5]; output 18. The singleton minima sum to 11; the longer subarrays contribute 4, 2 and 1, totaling 18.
Example 2: input nums = [2,3,1]; output 10. The six minima are 2, 3, 1, 2, 1 and 1, totaling 10.
Example 3: input nums = [3,1,2,4]; output 17. Singleton minima total 10; longer subarrays contribute 4, 2 and 1, giving 17.
Category: Stack. Topic: stack. Difficulty: Medium. Patterns: monotonic-stack.