当前位置:网站首页>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;
}
}
边栏推荐
- Direct dry goods, 100% praise
- 如何快速检查钢网开口面积比是否符合 IPC7525
- 目标跟踪常见训练数据集格式
- Personal notes of graphics (3)
- Leetcode-136- number that appears only once (solve with XOR)
- "The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."
- Introduction to ThinkPHP URL routing
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
- pycharm 终端部启用虚拟环境
- 整理几个重要的Android知识,高级Android开发面试题
猜你喜欢
Talk about the realization of authority control and transaction record function of SAP system
二叉搜索树(特性篇)
[vulnhub range] thales:1
如何快速检查钢网开口面积比是否符合 IPC7525
Record the migration process of a project
运算符
【C 语言】 题集 of Ⅹ
Master this promotion path and share interview materials
[medical segmentation] attention Unet
【DesignMode】外观模式 (facade patterns)
随机推荐
Three. JS series (1): API structure diagram-1
Introduction and use of gateway
Geoserver2.18 series (5): connect to SQLSERVER database
掌握这个提升路径,面试资料分享
ByteDance Android gold, silver and four analysis, Android interview question app
Laravel post shows an exception when submitting data
模拟Servlet的本质
值得一看,面试考点与面试技巧
How can laravel get the public path
Find tags in prefab in unity editing mode
【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
Sort out several important Android knowledge and advanced Android development interview questions
Balanced binary tree (AVL)
两类更新丢失及解决办法
JS 模块化
整理几个重要的Android知识,高级Android开发面试题
Opencv personal notes
logback.xml配置不同级别日志,设置彩色输出
HAVE FUN | “飞船计划”活动最新进展
"The" "PIP" "entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program."