题目
给你一个整数数组 nums 。nums 中,子数组的 范围 是子数组中最大元素和最小元素的差值。
返回 nums 中 所有 子数组范围的 和 。
子数组是数组中一个连续 非空 的元素序列。
示例 1:
输入:nums = [1,2,3] 输出:4 解释:nums 的 6 个子数组如下所示: [1],范围 = 最大 - 最小 = 1 - 1
= 0 [2],范围 = 2 - 2 = 0 [3],范围 = 3 - 3 = 0 [1,2],范围 = 2 - 1 = 1 [2,3],范围 = 3 - 2 = 1 [1,2,3],范围 = 3 - 1 = 2 所有范围的和是 0 + 0 + 0 + 1 + 1
- 2 = 4 示例 2:
输入:nums = [1,3,3] 输出:4 解释:nums 的 6 个子数组如下所示: [1],范围 = 最大 - 最小 = 1 - 1
= 0 [3],范围 = 3 - 3 = 0 [3],范围 = 3 - 3 = 0 [1,3],范围 = 3 - 1 = 2 [3,3],范围 = 3 - 3 = 0 [1,3,3],范围 = 3 - 1 = 2 所有范围的和是 0 + 0 + 0 + 2 + 0
- 2 = 4 示例 3:
输入:nums = [4,-2,-3,4,1] 输出:59 解释:nums 中所有子数组范围的和是 59
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sum-of-subarray-ranges
暴力尝试
这道题目与【我理解的算法 - 907.子数组的最小值之和(单调栈经典例题)】的题目主题思路是一样的,但是比上面的题目更难一些,其实就是不仅要求最小值之和还要求最大值之和,下面我们会说明为什么要这样,当然我们先看看暴力解
public long subArrayRanges(int[] nums) {
int n = nums.length;
long ans = 0;
for(int i = 0; i < n; i++){
int maxValue = nums[i];
int minValue = nums[i];
for(int j = i; j < n; j++){
maxValue = Math.max(maxValue, nums[j]);
minValue = Math.min(minValue, nums[j]);
ans += maxValue - minValue;
}
}
return ans;
}
这个解法的思路是依靠i和j划出一个子数组的左右边界,然后每次更新最大值和最小值,并计算最大值与最小值的差之和,这个解法也是能过的,但不是最优解,最优解其实还是用到了单调栈,下面来看看
单调栈
我们这道题目要使用单调栈我们首先要发现一个规律,我们从题目和示例中可以看到,范围即为 子 数 组 中 的 最 大 值 − 子 数 组 中 的 最 小 值 子数组中的最大值 - 子数组中的最小值 子数组中的最大值−子数组中的最小值,然后把所有的子数组的范围加起来,就是范围和了,在这边有个技巧是这道题目的关键所在了,我们可以假设有n个子数组,再假设每个子数组的范围等于了: s 1 = a 1 m a x − b 1 m i n s1 = a1_{max} - b1_{min} s1=a1max−b1min,所有子数组的范围和等于了: s 1 + s 2 + s 3 + . . . = a 1 m a x − b 1 m i n + a 2 m a x − b 2 m i n + a 3 m a x − b 3 m i n + . . . s1+s2+s3+... = a1_{max} - b1_{min} + a2_{max} - b2_{min} + a3_{max} - b3_{min} + ... s1+s2+s3+...=a1max−b1min+a2max−b2min+a3max−b3min+...,那么这个公式再把它合并一下就等于了: ( a 1 m a x + a 2 m a x + a 3 m a x + . . . ) − ( b 1 m i n + b 2 m i n + b 3 m i n ) (a1_{max}+a2_{max}+a3_{max}+...) - (b1_{min} + b2_{min} + b3_{min}) (a1max+a2max+a3max+...)−(b1min+b2min+b3min) ,这样就看出来了吧,即我们最终公式变成 所 有 子 数 组 的 范 围 和 = 所 有 子 数 组 的 最 大 值 之 和 − 所 有 子 数 组 的 最 小 值 之 和 所有子数组的范围和 = 所有子数组的最大值之和 - 所有子数组的最小值之和 所有子数组的范围和=所有子数组的最大值之和−所有子数组的最小值之和,这样这道题目就简单了,子数组的最小值之和我们上一道题目刚刚做过,没难度哈,直接拿来就行,那么子数组的最大值之和呢?其实也简单,我们用单调递增栈计算出了子数组的最小值之和,那么我们用单调递减栈就可以求得子数组的最大值之和了,这一步的证明大家可以用我上面的解题思路自行证实一下,所以我们用2个栈即可以求得最终的结果,(另外对单调栈还不了解的童鞋,可以先看我另一篇对单调栈有详细说明的文章【我理解的算法 - 907.子数组的最小值之和(单调栈经典例题)】,看了就会全明白了),代码如下:
public long subArrayRanges(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int minStackTop = -1, maxStackTop = -1, n = nums.length;
int[] minStack = new int[n], maxStack = new int[n];
long minSum = 0, maxSum = 0;
for(int i = 0; i < nums.length; i++){
while(minStackTop >= 0 && nums[i] < nums[minStack[minStackTop]]){
minSum += (long) calculate(minStack, minStackTop, i, nums) * nums[minStack[minStackTop]];
--minStackTop;
}
minStack[++minStackTop] = i;
while(maxStackTop >= 0 && nums[i] > nums[maxStack[maxStackTop]]){
maxSum += (long) calculate(maxStack, maxStackTop, i, nums) * nums[maxStack[maxStackTop]];
--maxStackTop;
}
maxStack[++maxStackTop] = i;
}
while(minStackTop >= 0){
minSum += (long) calculate(minStack, minStackTop, n, nums) * nums[minStack[minStackTop]];
--minStackTop;
}
while(maxStackTop >= 0){
maxSum += (long) calculate(maxStack, maxStackTop, n, nums) * nums[maxStack[maxStackTop]];
--maxStackTop;
}
return maxSum - minSum;
}
public int calculate(int[] stack, int top, int curLength, int[] nums){
int index = stack[top];
int leftIndex = top - 1 < 0 ? -1 : stack[top - 1];
int rightIndex = curLength;
int leftCount = index - leftIndex - 1;
int rightCount = rightIndex - index - 1;
return (leftCount + 1) * (rightCount + 1);
}
看着复杂,其实思路挺清晰的,其实就是做了2遍单调栈,分别求了子数组的最大值之和和最小值之和,这样你理解了吗?
最后还望各位兄弟姐妹们点个赞,关个注,更多的我理解的内容我还会陆续和大家分享的,谢谢大家!