LC-1031. 两个非重叠子数组的最大和(前缀和 + 枚举(滑动窗口))

文章介绍了如何使用前缀和结合枚举及滑动窗口优化的方法,解决LeetCode中的1031题,即寻找两个给定长度且不重叠的子数组,使它们的元素和最大。文章提供了不同复杂度的解决方案,并展示了如何减少时间复杂度至O(n)。
摘要由CSDN通过智能技术生成

1031. 两个非重叠子数组的最大和

难度中等168收藏分享切换为英文接收动态反馈

给你一个整数数组 nums 和两个整数 firstLensecondLen,请你找出并返回两个非重叠 子数组 中元素的最大和*,*长度分别为 firstLensecondLen

长度为 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值