当前位置:网站首页>LeetCode 1031. Maximum sum of two non overlapping subarrays
LeetCode 1031. Maximum sum of two non overlapping subarrays
2022-07-07 16:58:00 【@Little safflower】
Problem description
Give an array of nonnegative integers A , Return two non overlapping ( continuity ) The maximum sum of elements in a subarray , The length of the subarray is L and M.( What needs to be clarified here is , Long for L The subarray of can appear at length as M Before or after the subarray of .)
In form , Return to the biggest V, and V = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1]) And meet one of the following conditions :
0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
0 <= j < j + M - 1 < i < i + L - 1 < A.length.
Example 1:
Input :A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
Output :20
explain : In a selection of subarrays ,[9] The length is 1,[6,5] The length is 2.
Example 2:Input :A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
Output :29
explain : In a selection of subarrays ,[3,8,1] The length is 3,[8,9] The length is 2.
Example 3:Input :A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
Output :31
explain : In a selection of subarrays ,[5,6,0,9] The length is 4,[0,3,8] The length is 3.
Tips :
L >= 1
M >= 1
L + M <= A.length <= 1000
0 <= A[i] <= 1000source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-sum-of-two-non-overlapping-subarrays
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
java
class Solution {
public int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
int n = nums.length;
// The prefix and
int[] sum = new int[n + 1];
for(int i = 0;i < n;i++){
sum[i + 1] = sum[i] + nums[i];
}
// In exchange for
if(firstLen > secondLen){
firstLen ^= secondLen;
secondLen ^= firstLen;
firstLen ^= secondLen;
}
int[][] dp = new int[n + 1][2];
int ans = 0;
// From firstLen Start
for(int i = firstLen;i <= n;i++){
// The current element has been closed before firstLen And
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;
}
}
边栏推荐
- QML初学
- 01tire+ chain forward star +dfs+ greedy exercise one
- LeetCode 152. 乘积最大子数组 每日一题
- Tidb cannot start after modifying the configuration file
- The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
- 最新Android面试合集,android视频提取音频
- AutoLISP series (2): function function 2
- Cesium (4): the reason why gltf model is very dark after loading
- 最新高频Android面试题目分享,带你一起探究Android事件分发机制
- 打造All-in-One应用开发平台,轻流树立无代码行业标杆
猜你喜欢
AutoLISP series (2): function function 2
字节跳动高工面试,轻松入门flutter
最新Android面试合集,android视频提取音频
AutoLISP series (3): function function 3
[medical segmentation] attention Unet
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
二叉搜索树(特性篇)
数据中台落地实施之法
谈谈 SAP 系统的权限管控和事务记录功能的实现
随机推荐
一文读懂数仓中的pg_stat
掌握这套精编Android高级面试题解析,oppoAndroid面试题
Direct dry goods, 100% praise
最新Android面试合集,android视频提取音频
ATM系统
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
Laravel5.1 Routing - routing packets
Personal notes of graphics (4)
Lowcode: four ways to help transportation companies enhance supply chain management
node:504报错
最新2022年Android大厂面试经验,安卓View+Handler+Binder
Sqlserver2014+: create indexes while creating tables
Geoserver2.18 series (5): connect to SQLSERVER database
LeetCode 152. 乘积最大子数组 每日一题
Advanced C language -- function pointer
skimage学习(1)
直接上干货,100%好评
logback. XML configure logs of different levels and set color output
两类更新丢失及解决办法
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑