当前位置:网站首页>LeetCode 1031. 两个非重叠子数组的最大和 每日一题
LeetCode 1031. 两个非重叠子数组的最大和 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给出非负整数数组 A ,返回两个非重叠(连续)子数组中元素的最大和,子数组的长度分别为 L 和 M。(这里需要澄清的是,长为 L 的子数组可以出现在长为 M 的子数组之前或之后。)
从形式上看,返回最大的 V,而 V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) 并满足下列条件之一:
0 <= i < i + L - 1 < j < j + M - 1 < A.length, 或
0 <= j < j + M - 1 < i < i + L - 1 < A.length.
示例 1:
输入:A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
输出:20
解释:子数组的一种选择中,[9] 长度为 1,[6,5] 长度为 2。
示例 2:输入:A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
输出:29
解释:子数组的一种选择中,[3,8,1] 长度为 3,[8,9] 长度为 2。
示例 3:输入:A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
输出:31
解释:子数组的一种选择中,[5,6,0,9] 长度为 4,[0,3,8] 长度为 3。
提示:
L >= 1
M >= 1
L + M <= A.length <= 1000
0 <= A[i] <= 1000来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
java
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
//前缀和
int[] sum = new int[n + 1];
for(int i = 0;i < n;i++){
sum[i + 1] = sum[i] + nums[i];
}
//交换
if(firstLen > secondLen){
firstLen ^= secondLen;
secondLen ^= firstLen;
firstLen ^= secondLen;
}
int[][] dp = new int[n + 1][2];
int ans = 0;
//从第firstLen开始
for(int i = firstLen;i <= n;i++){
//已当前元素为结束的前firstLen的和
int s1 = sum[i] - sum[i - firstLen];
dp[i][0] = Math.max(dp[i - 1][0],s1);
ans = Math.max(ans,s1 + dp[i - firstLen][1]);
if(i >= secondLen){
int s2 = sum[i] - sum[i - secondLen];
dp[i][1] = Math.max(dp[i - 1][1],s2);
ans = Math.max(ans,s2 + dp[i - secondLen][0]);
}
}
return ans;
}
}
边栏推荐
- 【MySql进阶】索引详解(一):索引数据页结构
- 谎牛计数(春季每日一题 53)
- Sqlserver2014+: create indexes while creating tables
- Tragedy caused by deleting the console statement
- Set the route and optimize the URL in thinkphp3.2.3
- How does laravel run composer dump autoload without emptying the classmap mapping relationship?
- [medical segmentation] attention Unet
- JS中null NaN undefined这三个值有什么区别
- time标准库
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
猜你喜欢
![[designmode] facade patterns](/img/79/cde2c18e2ec8b08697662ac352ff90.png)
[designmode] facade patterns

Pycharm terminal enables virtual environment

null == undefined

DNS 系列(一):为什么更新了 DNS 记录不生效?

Master this promotion path and share interview materials

水平垂直居中 方法 和兼容

Imitate the choice of enterprise wechat conference room

全网“追杀”钟薛高

Sort out several important Android knowledge and advanced Android development interview questions

模块六
随机推荐
[designmode] template method pattern
Sqlserver2014+: create indexes while creating tables
Common training data set formats for target tracking
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Statistical learning method -- perceptron
Laravel constructor and middleware execution order
Build an all in one application development platform, light flow, and establish a code free industry benchmark
直接上干货,100%好评
【Android -- 数据存储】使用 SQLite 存储数据
Master this promotion path and share interview materials
如何选择合适的自动化测试工具?
【PHP】PHP接口继承及接口多继承原理与实现方法
Laravel post shows an exception when submitting data
ATM系统
os、sys、random标准库主要功能
Have fun | latest progress of "spacecraft program" activities
[vulnhub range] thales:1
Temperature sensor chip used in temperature detector
47_ Contour lookup in opencv cv:: findcontours()