文章目录
1031. 两个非重叠子数组的最大和
难度中等168收藏分享切换为英文接收动态反馈
给你一个整数数组 nums
和两个整数 firstLen
和 secondLen
,请你找出并返回两个非重叠 子数组 中元素的最大和*,*长度分别为 firstLen
和 secondLen
。
长度为 firstLen
的子数组可以出现在长为 secondLen
的子数组之前或之后,但二者必须是不重叠的。
子数组是数组的一个 连续 部分。
示例 1:
输入:nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
输出:20
解释:子数组的一种选择中,[9] 长度为 1,[6,5] 长度为 2。
示例 2:
输入:nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
输出:29
解释:子数组的一种选择中,[3,8,1] 长度为 3,[8,9] 长度为 2。
示例 3:
输入:nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
输出:31
解释:子数组的一种选择中,[5,6,0,9] 长度为 4,[0,3,8] 长度为 3。
提示:
1 <= firstLen, secondLen <= 1000
2 <= firstLen + secondLen <= 1000
firstLen + secondLen <= nums.length <= 1000
0 <= nums[i] <= 1000
前缀和 + 枚举
枚举每一个firstlen
长度,在当前枚举到的窗口之外,再遍历每一个secondlen
长度的窗口,求最大值
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
int[] pre = new int[n+1];
for(int i = 0; i < n; i++){
pre[i+1] = pre[i] + nums[i];
}
int res = 0;
for(int i = firstLen; i <= n; i++){
// 当前长度为firstLen的窗口 和
int sumf = pre[i] - pre[i-firstLen];
// 不在firstLen窗口内的 最大secondLen窗口大小的和
int sums = 0;
for(int j = secondLen; j < i - firstLen; j++){
sums = Math.max(sums, pre[j] - pre[j - secondLen]);
}
for(int j = i + secondLen; j <= n; j++){
sums = Math.max(sums, pre[j] - pre[j - secondLen]);
}
res = Math.max(res, sumf + sums);
}
return res;
}
}
前缀和 + 枚举(滑动窗口)O(n)
做法
https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays/solution/tu-jie-mei-you-si-lu-yi-zhang-tu-miao-do-3lli/
上面的做法在枚举每一个firstLen
窗口时还需要遍历除这个窗口外的其他长度为secondLen
的窗口,进一步优化,当firstLen
窗口往右移动时,窗口的左边大小也是不断增加的,并且不会重复,可以利用这一点,变为O(n)的做法。
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
int[] pre = new int[n+1];
for(int i = 0; i < n; i++){
pre[i+1] = pre[i] + nums[i];
}
return Math.max(f(pre, firstLen, secondLen), f(pre, secondLen, firstLen));
}
public int f(int[] s, int firstLen, int secondLen){
// 左窗口的和maxSumA,这个和 在右窗口往右移动时判断大小,确保不会和右窗口重复
int maxSumA = 0, res = 0;
for(int i = firstLen + secondLen; i < s.length; i++){
maxSumA = Math.max(maxSumA, s[i - secondLen] - s[i - secondLen - firstLen]);
res = Math.max(res, maxSumA + s[i] - s[i-secondLen]);
}
return res;
}
}
将先a后b和先b后a放到一个循环中执行
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
int[] s = new int[n+1];
for(int i = 0; i < n; i++){
s[i+1] = s[i] + nums[i];
}
int res = 0, maxSumA = 0, maxSumB = 0;
for(int i = firstLen + secondLen; i <= n; i++){
maxSumA = Math.max(maxSumA, s[i-secondLen] - s[i - secondLen - firstLen]);
maxSumB = Math.max(maxSumB, s[i-firstLen] - s[i - firstLen - secondLen]);
res = Math.max(res, Math.max(maxSumA + s[i] - s[i - secondLen], // 左 a 右 b
maxSumB + s[i] - s[i - firstLen])); // 左 b 右 a
}
return res;
}
public int f(int[] s, int firstLen, int secondLen){
// 左窗口的和maxSumA,这个和 在右窗口往右移动时判断大小,确保不会和右窗口重复
int maxSumA = 0, res = 0;
for(int i = firstLen + secondLen; i < s.length; i++){
maxSumA = Math.max(maxSumA, s[i - secondLen] - s[i - secondLen - firstLen]);
res = Math.max(res, maxSumA + s[i] - s[i-secondLen]);
}
return res;
}
}