LeetCode1031. Maximum Sum of Two Non-Overlapping Subarrays

OpenClaw简介--windows系统安装OpenClaw 在windows安装OpenClaw实现代替人工操作。步骤流程详细,图文并茂。 阅读详情

[Med] LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays

链接:https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/

题目描述
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)

Formally, return the largest V for which V = (A[i] + A[i+1] + … + A[i+L-1]) + (A[j] + A[j+1] + … + A[j+M-1]) and either:

0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
0 <= j < j + M - 1 < i < i + L - 1 < A.length.

给出非负整数数组 A ,返回两个非重叠(连续)子数组中元素的最大和,子数组的长度分别为 L 和 M。(这里需要澄清的是,长为 L 的子数组可以出现在长为 M 的子数组之前或之后。)

Example 1:

Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.

Example 2:

Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.

Example 3:

Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.



Tag: Pre-Sum
解题思路
这道题目让我们找到两个不相交且连续的子数组,使得这两个数组的和可以组成最大值。一看就是标准版的pre-sum来解,可惜一开始没有想到O(n)的做法,只有一个O(n^2)的做法。

解法一:
这个做法是lee神给出来的,我加上了自己的解释
Lsum, sum of the last L elements
Msum, sum of the last M elements

Lmax, max sum of contiguous L elements before the last M elements.
Mmax, max sum of contiguous M elements before the last L elements/

首先解释一下这个算法的核心,我们首先还是需要计算这个数组的pre-sum。在这里我们不需要新开一个数组,而是直接在原数组上面更新就可以了。

然后我们需要三个变量,第一个用来储存结果,res。第二个和第三个叫Lmax和Mmax。作用如下,
我们首先将res赋值为A[L+M-1],意思是初始化结果为前L+M个数的和。Lmax是初始化为前L个数的和,Mmax初始化为前M个数的和。假设我们遍历到了位置A[i]。我们采用如下手段更新Lmax的值,也就是截止目前为止长度为L的subarray的最大值。为了使得两个subarray位置不重合,我们需要给M先预留一个长度为M的subarray,这个subarray的区间位置是当前位置i到i-M这个范围。所以实际上Lmax是在[0, i-M]这一个范围内长度为L的最大subarray的和。那么我们每往后移动一位,对应的Lmax就需要和(A[i - M] - A[i - M - L])更新一下。我们对Mmax做同样的事情。那么可能我们可能就有问题了,这样对于Lmax和Mmax会不会没办法覆盖到所有的区间呢?其实不会的,因为无论如何,M和L都不能取重合的位置,所以实际操作下,我们对于每一个位置,都分别计算在固定M subarray位置的情况下,可以找到的最大的L subarray数值是多少。和固定L subarray位置的情况下,可以找到的最大的M subarray数值是多少。在每一步的最后我们将提前固定的M Subarray和Lmax取和, 和固定的L subarray和Mmax取和,最后和re进行比较。求得在i位的最大值。如此重复直到最后一位。

class Solution {
   public int maxSumTwoNoOverlap(int[] A, int L, int M) {
        for (int i = 1; i < A.length; ++i)
            A[i] += A[i - 1]; 
        int res = A[L + M - 1], Lmax = A[L - 1], Mmax = A[M - 1];
        for (int i = L + M; i < A.length; ++i) {
            Lmax = Math.max(Lmax, A[i - M] - A[i - M - L]);
            Mmax = Math.max(Mmax, A[i - L] - A[i - L - M]);
            res = Math.max(res, Math.max(Lmax + A[i] - A[i - M], Mmax + A[i] - A[i - L]));
        }
        return res;
    }
}

解法二:
这是第一次做的,相当于O(n2)了,但是实际上要快一些因为对于长度为len的数组A来说,只是计算了 (len-M-L)次.这个做法的意思是,我们先固定L的窗口位置,然后遍历除L窗口位置的整个数组。寻找与L匹配的最大的M的窗口和。

class Solution {
    public int maxSumTwoNoOverlap(int[] A, int L, int M) {
        int res =0;
        int[] preSum = new int[A.length];
        int pre =0;
        for(int i=0; i<A.length; i++){
            pre+=A[i];
            preSum[i]  = pre;
        }
        
        for(int i=0; i<= A.length-L; i++){
            int lSum = preSum[i+L-1]- (i== 0? 0: preSum[i-1]);
            int mSum = 0;
            for(int j = i+L; j<= A.length-M; j++){
                mSum = Math.max(mSum, preSum[j+M-1]- (j== 0? 0: preSum[j-1]));
            }
            for(int j = 0; j<= i-M; j++){
                mSum = Math.max(mSum, preSum[j+M-1]- (j== 0? 0: preSum[j-1]));
            }
    
            res = Math.max(res, lSum+mSum);
        }
        
        return res;
    }
}

解法三:

这个也是O(n)的时间和空间,但是相对比较复杂。我的思路是,这道题目的关键点其实在于,假设当我们以M窗口范围进行遍历
原数组的时候,如何最快速度找到这个窗口之外的位置上的最大L窗口喝是多少。反过来,以L窗口的范围遍历的时候,也要找到除其之外最大的M窗口的和。
那么首先还是计算数组的Pre-sum。然后此时我们创建两个长度为A.length()(ps,只是为了方便计算)的数组,一个从后面往前计算L的窗口在每一位的最大值。比如说L = 2, 数组长度为8。 那我们要依次计算[6,7],[5,6],…,[0,1]的每一个长度为2的滑动窗口,并且在新数组maxOfL内对每一个位置更新我们从后往前遇到的 窗口长度为L的 最大和。
同样我们对maxOfL也做一样的事情。当我们有了这两个数组之后。此时我们从前往后,依次找到L窗口截止在位置i的最大值加上从maxOfM()中获取第i位的值,which is从后面往前计算的M最大值。可以将它当做以i点划分,左边是寻找L最大值的范围,右边从maxOfM()数组中获取到的M最大值。同样的事情我们也要从前往后对M也要做一遍。每一次我们都更新一下sum. 最后返回最大值。

class Solution {
    public int maxSumTwoNoOverlap(int[] A, int L, int M) {
        int[] presum = new int[A.length+1];
    
        for(int i=1; i<=A.length; i++){
            presum[i] = presum[i-1] + A[i-1];
        }
        
        int[] maxOfL = new int[A.length], maxOfM = new int[A.length];
        int mL =0, mM = 0;
        for(int i=A.length-1; i>=0; i--){
            if(i+L <= A.length){
                mL = Math.max(mL, presum[i+L]-presum[i]);
                maxOfL[i] = mL;
            }
            if(i+M <= A.length){
                mM = Math.max(mM, presum[i+M]-presum[i]);
                maxOfM[i] = mM;
            }
        }
        int sum =0, maxl =0, maxm = 0;
        for(int i=0; i<A.length; i++){
            if(i>=M){
                maxm = Math.max(maxm, presum[i]-presum[i-M]);
                sum = Math.max(sum, maxm+maxOfL[i]);
            }
            if(i >=L){
                maxl = Math.max(maxl, presum[i]-presum[i-L]);
                sum = Math.max(sum, maxl+maxOfM[i]);
            }
        }

        return sum;
        
    }
}
增益可控放大电路-电路与电子技术课程设计 阅读详情

相关推荐

科研软件|Adobe Photoshop 2024 (PS 2024)

科研软件|Adobe Photoshop 2024 (PS 2024)

m0_55499682的博客 5546

晨哥Leetcode 1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray co...

weixin_45588823的博客 341

电工电子实验——3BIT延时电路的设计与实现

设计一个 3BIT延时电路。 设计分析: Ⅰ.根据触发器的特性,只有来两个触发脉冲时才能恢复到原来的状态。也就是说触发器的输出信号比触发信号延迟了一个触发脉冲周期即1bit。据此3bit就需要3个触发器。 Ⅱ.输出要可控就要加一控制器,可用选择器实现。 Ⅲ.为便于观察输出信号的延迟,可设计一个F1信号。

qq_55543315的博客 1131

LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays滑动窗口 前缀和

Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray could occur before or after theM-length subarray.) Formall...

taoqick的专栏 309

ROS2_IGH方案配置(二)——Ubuntu22.04 实时内核性能验证与调优

本文详细介绍了在Ubuntu22.04系统上配置ROS2_IGH方案时,如何验证和调优PREEMPT_RT实时内核性能。通过使用cyclictest工具进行基准测试和压力测试,分析中断与硬件延迟,并优化内核参数,确保系统满足工业控制领域对硬实时性的苛刻要求。文章还提供了针对ROS2的特殊优化建议和性能监控方案,帮助开发者实现毫秒级延迟的稳定控制。

weixin_30791095的博客 228

STM32内存大小与地址的对应关系以及计算方法

1、概述 在计算机中要访问存储区,都是通过地址来进行访问的,包括所有的外设(比如串口、内存、硬盘等),都对应有一个访问地址,地址就相当于门牌号,有了门牌号,我们才知道操作的是哪一个区域、哪一个外设。 最被大家熟知的是计算机内存,计算机内存有4G、8G,16G等等,对于程序员而言,要在代码中操作这些内存,肯定得知道内存的地址,此外,还需要会计算地址与内存大小的计算方法与对应关系。STM32作为微

Ace's Blog 4万+

1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray coul...

Do not mess it up 374

rk3588上适配wifi模组aic8800

记录一下在rk3588上调试aic8800,调试分为Debian11以及Android12两个平台,这里主要是讲过程以及修改的过程,以补丁的形式来提供解决方法。在Debian11下主要通过配置设备树、编译驱动并自动加载固件完成调试,详细介绍了设备树修改、驱动编译脚本调整和驱动加载流程。

m0_60805802的博客 1011

[LeetCode] 1031: Maximum sum of two non-overlapping subarrays

原题目 Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray ...

weixin_33691700的博客 335

手把手保姆级|Python爬取微信公众号文章完整版,反反爬策略拉满,附可运行完整代码(永久可用)

做Python爬虫的朋友都知道,爬取微信公众号文章 绝对是爬虫领域的「香饽饽」也是「硬骨头」。一方面,微信公众号的文章是极具价值的数据源:不管是自媒体从业者做竞品分析、新媒体运营者整理干货、还是个人学习收集资料,都需要批量爬取公众号文章;另一方面,微信的反爬机制堪称全网最严苛之一 ——腾讯对微信生态的风控做到了极致,动态渲染页面、高频IP封禁、Cookie短时效失效、请求头严格校验、自动化工具秒识别、频繁请求直接拉黑,这些反爬手段让90%的爬虫新手望而却步:要么爬了几篇就被封IP,要么Cookie失效爬取中

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 2645

Vue - 快速入门,这一套就够了!(Vue core + 案例 + 效果演示)

前置条件:掌握 HTML、CSS、JavaScript、AJAX.工具:VsCode.插件:Live Server(浏览器实时预览)Ps:建议都提前下载下来,没网的时候也能用.

CYK_byte的博客 4万+

【leetcode每日刷题】1031. Maximum Sum of Two Non-Overlapping Subarrays

Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subarray coul...

菜菜鸟的博客 260

2024年广东省职业院校技能大赛“区块链技术应用”任务书—区块链应用系统开发参考答案

摘要 本文档提供了2024年广东省职业院校技能大赛"区块链技术应用"项目中区块链电子签章前端功能开发的参考答案。主要内容包括:1)注册页面开发,实现身份证号码双向绑定和注册请求发送;2)登录页面开发,实现用户认证、状态保存和页面跳转;3)个人印章页面开发,实现注销功能和印章信息展示。每个任务均包含具体实现代码片段和功能测试要点,涵盖Vue.js框架下的数据绑定、axios请求处理和本地存储等关键技术点。

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 351

LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays

链接:https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays/ 思路 假设L子串在M子串左边。反向迭代A,到达位置i时,计算M子串[i, i+M)总和,并记录到i时已计算的M子串总和的最大值maxMSum[i]。然后正向迭代A,到达位置i时,此时已知的两串总和最大为maximal,计算L子串[i, i+L...

zjj1149的博客 470

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【leetcode】1031 maximum sum of two non overlapping subarrays

题目 https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays 给定一个数组A,找到其中不相重合的长为M及L的连续子序列,使两个子序列和最大,返回最大和即可 代码 v1.0 思路(暴力搜索可以想到的优化点): 计算出前后向长为n的子序列内max_m, max_l,然后便利找一下前向+后项一起啥时候最大 ...

u011104463的博客 214

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[leetcode] 1031. Maximum Sum of Two Non-Overlapping Subarrays

Description Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarra

Learning from the mistakes 254

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays

题目如下: Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length suba...

weixin_34253539的博客 111

leetcode 1031. Maximum Sum of Two Non-Overlapping Subarrays 解法 python

一.问题描述 Given an arrayAof non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengthsLandM. (For clarification, theL-length subar...

CSerwangjun的博客 809

LeetCode 1031. Maximum Sum of Two Non-Overlapping Subarrays 解题报告(python)

1031. Maximum Sum of Two Non-Overlapping Subarrays Maximum Sum of Two Non-Overlapping Subarrays python solution 题目描述 Given an array A of non-negative integers, return the maximum sum of elements in ...

Orientliu96的博客 613

LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays

This problem requires finding the maximum sum of two non-overlapping subarrays of given lengths in an array. The solution involves considering both possible orders of the subarrays (firstLen before secondLen or vice versa). For each position, it calculates

Made in Code 432

【Leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays

可以枚举第二个子数组取哪段,枚举第二个子数组的时候,动态求一下第一个子数组可以取到的最大和。中取两个不相交的子数组,长度分别是。,问两个子数组的总和最大值。可以先考虑左边那个子数组长。

数学、算法爱好者的博客 232

No.278 - LeetCode[1031] Maximum Sum of Two Non-Overlapping Subarrays - 前缀和

思路如下代码,可以进一步内存优化,取消dp数组,直接用sum即可。 /* * @lc app=leetcode id=1031 lang=cpp * * [1031] Maximum Sum of Two Non-Overlapping Subarrays */ // @lc code=start class Solution { public: int maxSumTwoNoOverlap(vector<int>& nums, int firstLen, int se

ShellDawn的博客 231

Leetcode-1031 Maximum Sum of Two Non-Overlapping Subarrays(两个非重叠子数组的最大和)

1 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 2 class Solution 3 { 4 public: 5 int maxSumTwoNoOverlap(vector<int>& A, int L, int M) 6 { 7 ...

weixin_30906185的博客 150

leetcode 1031. Maximum Sum of Two Non-Overlapping Subarrays 两个无重叠子数组的最大和

Problem:计算前缀和,然后分别求出两种长度子数组的和,最后双循环,一种first在前,另外一种second在前,计算最大值。

邹九的个人博客 121

leetcode1031+Maximum Sum of Two Non-Overlapping Subarrays+dp-前缀和

【代码】leetcode1031+Maximum Sum of Two Non-Overlapping Subarrays+dp-前缀和。

小堃哥的专栏 267

[Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays...

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30662011的博客 167
下一篇: LeetCode 33. Search in Rotated Sorted Array
MrJustin
博客等级 码龄7年 1粉丝 42原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值