当前位置:网站首页>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;
}
}
边栏推荐
- Temperature sensor chip used in temperature detector
- The differences between exit, exit (0), exit (1), exit ('0 '), exit ('1'), die and return in PHP
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- Direct dry goods, 100% praise
- Ray and OBB intersection detection
- PHP has its own filtering and escape functions
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
- Pisa-Proxy SQL 解析之 Lex & Yacc
- 01tire+链式前向星+dfs+贪心练习题.1
- 深度监听 数组深度监听 watch
猜你喜欢

【DesignMode】外观模式 (facade patterns)

最新Android面试合集,android视频提取音频

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

Arduino 控制的双足机器人

pycharm 终端部启用虚拟环境

1亿单身男女“在线相亲”,撑起130亿IPO

The latest interview experience of Android manufacturers in 2022, Android view+handler+binder

谈谈 SAP 系统的权限管控和事务记录功能的实现

二叉搜索树(基操篇)

Prediction - Grey Prediction
随机推荐
Geoserver2.18 series (5): connect to SQLSERVER database
Temperature sensor chip used in temperature detector
Opencv configuration 2019vs
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
【PHP】PHP接口继承及接口多继承原理与实现方法
Opencv personal notes
Vs2019 configuration matrix library eigen
OpenGL personal notes
Three. JS series (3): porting shaders in shadertoy
Asyncio concept and usage
URL和URI的关系
Read PG in data warehouse in one article_ stat
Usage of config in laravel
Deep listening array deep listening watch
正在准备面试,分享面经
Build an all in one application development platform, light flow, and establish a code free industry benchmark
【图像传感器】相关双采样CDS
Balanced binary tree (AVL)
二叉搜索树(基操篇)
Horizontal and vertical centering method and compatibility