首页 > 其他分享 >力扣---1031. 两个非重叠子数组的最大和

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

时间:2023-04-26 21:34:58浏览次数:47  
标签:力扣 nums int max len firstLen --- secondLen 1031

给你一个整数数组 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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


 

一眼动规,虽然不知道到底该咋动规。。。

最近动规题挺多的,懵懵懂懂就写出来了,虽然还是没能弄清楚自己到底咋动规的。

可以发现,可以将当前数组分为左右两部分,分别存放 first 和 second 。

此时,可以想到利用辅助数组,分别存放当前位置前的最大值,以及后面的最大值。

又由于 first 和 second 的前后顺序不固定,所以还需要知道 second 在前,first 在后的情况。

代码如下:

class Solution {
    public int maxSumTwoNoOverlap (int[] nums, int firstLen, int secondLen) {
        int len = nums.length;
        int[][] dp1 = new int[len][2];
        int max1 = 0;
        int max2 = 0;
        int tem1 = 0;
        int tem2 = 0;
        for (int i = 0; i < len; i ++) {
            if (i >= firstLen) {
                tem1 += nums[i] - nums[i - firstLen];
                max1 = Math.max(max1, tem1);
            } else {
                tem1 += nums[i];
                max1 = tem1;
            }
            dp1[i][0] = max1;
            if (i >= secondLen) {
                tem2 += nums[i] - nums[i - secondLen];
                max2 = Math.max(max2, tem2);
            } else {
                tem2 += nums[i];
                max2 = tem2;
            }
            dp1[i][1] = max2;
        }
        // System.out.println(Arrays.deepToString(dp1));
        int[][] dp2 = new int[len][2];
        max1 = 0;
        tem1 = 0;
        max2 = 0;
        tem2 = 0;
        for (int i = len - 1; i >= 0; i --) {
            dp2[i][0] = max1;
            if (i + secondLen < len) {
                tem1 += nums[i] - nums[i + secondLen];
                max1 = Math.max(max1, tem1);
            } else {
                tem1 += nums[i];
                max1 = tem1;
            }
            dp2[i][1] = max2;
            if (i + firstLen < len) {
                tem2 += nums[i] - nums[i + firstLen];
                max2 = Math.max(max2, tem2);
            } else {
                tem2 += nums[i];
                max2 = tem2;
            }
        }
        // System.out.println(Arrays.deepToString(dp2));
        int res = 0;
        for (int i = 0; i < len; i ++) {
            res = Math.max(res, dp1[i][0] + dp2[i][0]);
            res = Math.max(res, dp1[i][1] + dp2[i][1]);
        }
        return res;
    }
}

 由于两次的操作相同,只不过是改变了 firstLen 和 secondLen 的顺序。因此,可以用函数来简化代码。

而且也可以在从后往前给辅助数组添加数据时动态遍历。也可以直接节省掉第二个辅助数组的空间。

class Solution {
    public int maxSumTwoNoOverlap (int[] nums, int firstLen, int secondLen) {
        return Math.max(getRes(nums, firstLen, secondLen), getRes(nums, secondLen, firstLen));
    }

    private int getRes (int[] nums, int firstLen, int secondLen) {
        int len = nums.length;
        int[] dp = new int[len];
        int max = 0;
        int tem = 0;
        for (int i = 0; i < len; i++) {
            if (i >= firstLen) {
                tem += nums[i] - nums[i - firstLen];
                max = Math.max(max, tem);
            } else {
                tem += nums[i];
                max = tem;
            }
            dp[i] = max;
        }
        max = 0;
        tem = 0;
        int res = 0;
        for (int i = len - 1; i >= 0; i--) {
            res = Math.max(res, dp[i] + max);
            if (i + secondLen < len) {
                tem += nums[i] - nums[i + secondLen];
                max = Math.max(max, tem);
            } else {
                tem += nums[i];
                max = tem;
            }
        }
        return res;
    }
}

 

标签:力扣,nums,int,max,len,firstLen,---,secondLen,1031
From: https://www.cnblogs.com/allWu/p/17357419.html

相关文章

  • MFC-TextOut绘制文本
     HDChdc=::GetDC(m_hWnd);LOGFONTlf={0};lf.lfWeight=16;//平均宽度lf.lfHeight=40;//字体高度lf.lfCharSet=GB2312_CHARSET;//字符集lstrcpy(lf.lfFaceName,_T("宋体"));HFONThfont=::CreateFontIndirect(&lf)......
  • Django模型层(一) (测试环境搭配 常见的十几种查询方法-ORM关键字 ORM执行SQL语句
    目录一、测试环境搭配切换数据库自带的sqlite3数据库对时间字段不敏感有时候会展示错乱,所以我们习惯切换成常见的数据库比如MySQLdjangoorm并不会自动帮你创建库,所以需要提前准备好!单独搭配测试环境单独测试django某个功能层,默认不允许单独测试某个py文件,如果想要测试......
  • 04-3 气体燃料燃烧:湍流火焰燃烧
    湍流燃烧火焰特点发光区较厚,火焰轮廓较模糊,存在弯曲皱折火焰面有抖动,火焰长度也显著地缩短燃烧过程中伴有噪声在湍流中预混可燃气体的火焰传播速度比层流时大许多倍Re数对火焰传播速度的影响Re﹤2300,属于层流燃烧,火焰传播速度的大小与Re数无关;2300≤Re≤6000,火焰传播速度......
  • jQuery HTML-删除元素
    <!DOCTYPEhtml><html><head><metacharset="utf-8"/><title></title><scriptsrc="../../Scripts/jquery-3.4.1.min.js"></script><scriptsrc="delete.js">&l......
  • Solution Set - “让季节停止哽咽”
    目录0.「CTT2017」「洛谷P4004」Helloworld!1.「CTT2017」「洛谷P4006」小Y和二叉树2.「CTT2017」「洛谷P4226」避难所3.「AGC023F」01onTree4.「AGC024C」SequenceGrowingEasy5.「UR#1」「UOJ#21」缩进优化6.「JOISC2022」「LOJ#3694」一流团子师傅7.「JOISC......
  • 第7章-输入输出系统
    第7章输入/输出系统7.2I/O接口7.2.1I/O接口的功能进行地址译码和设备选择实现主机和外设的通信联络控制实现数据缓冲信号格式的转换传送控制命令和状态信息7.2.2I/O接口的基本结构7.2.4IO端口及其编址1.统一编制把IO端口当做存储器的单元进行地址分配,用统一的......
  • EXP-00026: 指定了冲突模式
    C:\>exphibernate/hibernate@orclfile=c:\emp.dmpfull=ytables=(emp)Export:Release10.2.0.1.0-Productionon星期五5月922:57:132014Copyright(c)1982,2005,Oracle.Allrightsreserved.连接到:OracleDatabase10gEnterpriseEditionRelease10.......
  • python打包工具-Nuitka
    nuitka将python源码转成C++(这里得到的是二进制的pyd文件,防止了反编译),然后再编译成可执行文件。提高安全性和运行速度。github:https://github.com/2267770481/cython_test安装pipinstallnuitkapipinstallordered-set#加速编译pipinstallzstandard#onefile时压缩文件......
  • D. Super-Permutation
    D.Super-PermutationApermutationisasequence$n$integers,whereeachintegerfrom$1$to$n$appearsexactlyonce.Forexample,$[1]$,$[3,5,2,1,4]$,$[1,3,2]$arepermutations,while$[2,3,2]$,$[4,3,1]$,$[0]$arenot.Givenapermutation$a$,wec......
  • 深入java虚拟机 - 垃圾收集 - 引用计数收集器
         引用计数是垃圾收集的早期策略。在这种方法中,堆中每一个对象都有一个引用计数。一个对象被创建了,并且指向该对象的引用被分配给一个变量,这个对象的引用计数被置为1。当任何其他变量被赋值为对这个对象的引用时,计数加1。当一个对象的引用超过了......