Sum of Subarray Ranges
Given a nonempty integer array nums, return the sum of the range of every contiguous nonempty subarray. A subarray's range is its maximum value minus its…
Given a nonempty integer array `nums`, return the sum of the range of every contiguous nonempty subarray. A subarray's range is its maximum value minus its minimum value. Return the exact sum, with no modulus. Do not change the input array.
- `1 <= nums.length <= 1000` - `-1000000000 <= nums[i] <= 1000000000` - Use an exact wide integer result; the sum can exceed a signed 32-bit integer.
Example 1: input nums = [1,2,3]; output 4. Singletons contribute zero; the other intervals have ranges 1, 1 and 2.
Example 2: input nums = [1,3,3]; output 4. The intervals [1,3] and [1,3,3] contribute 2 each; all others have range zero.
Category: Stack. Topic: stack. Difficulty: Medium. Patterns: monotonic-stack.