2104. Sum of Subarray Ranges

2104. Sum of Subarray Ranges

Description

You are given an integer array nums. The range of a subarray of nums is the difference between the largest and smallest element in the subarray.

Return the sum of all subarray ranges of nums.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

1
2
3
4
5
6
7
8
9
10
Input: nums = [1,2,3]
Output: 4
Explanation: The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1,2], range = 2 - 1 = 1
[2,3], range = 3 - 2 = 1
[1,2,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.

Example 2:

1
2
3
4
5
6
7
8
9
10
Input: nums = [1,3,3]
Output: 4
Explanation: The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[3], range = 3 - 3 = 0
[3], range = 3 - 3 = 0
[1,3], range = 3 - 1 = 2
[3,3], range = 3 - 3 = 0
[1,3,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.

Example 3:

1
2
3
Input: nums = [4,-2,-3,4,1]
Output: 59
Explanation: The sum of all subarray ranges of nums is 59.

Constraints:

  • 1 <= nums.length <= 1000
  • -10^9 <= nums[i] <= 10^9

Follow-up: Could you find a solution with O(n) time complexity?

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
long long subArrayRanges(vector<int>& nums) {
long long res = 0;
int n = nums.size();
stack<int> maxVal, minVal;
for (int i = 0; i <= n; i++) {
while (!maxVal.empty() && (i == n || nums[maxVal.top()] < nums[i])) {
int prevMaxIdx = maxVal.top();
maxVal.pop();
int left = maxVal.empty() ? -1 : maxVal.top();
res += (long long)nums[prevMaxIdx] * (prevMaxIdx - left) * (i - prevMaxIdx);
}
maxVal.push(i);
while (!minVal.empty() && (i == n || nums[minVal.top()] > nums[i])) {
int prevMaxIdx = minVal.top();
minVal.pop();
int left = minVal.empty() ? -1 : minVal.top();
res -= (long long)nums[prevMaxIdx] * (prevMaxIdx - left) * (i - prevMaxIdx);
}
minVal.push(i);
}
return res;
}
};