[leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和
In a given array nums
of positive integers, find three non-overlapping subarrays with maximum sum.
Each subarray will be of size k
, and we want to maximize the sum of all 3*k
entries.
Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
Example:
Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
Note:
nums.length
will be between 1 and 20000.nums[i]
will be between 1 and 65535.k
will be between 1 and floor(nums.length / 3).
思路:
We need to find 3 subarrays
Let's say if I can find the 2nd subarray , then find the largest subarray on both left side and right side, problem solved.
代码:
class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int[] sum = new int[nums.length]; // sum[i] = num[i] + nums[i+1]...+nums[i+k-1];
int[] lef = new int[nums.length]; // lef[i] = before i, the max sum[];
int[] rig = new int[nums.length]; // rif[i] = after i, the max sum[];
int[] IndexL = new int[nums.length];
int[] IndexR = new int[nums.length];
int total = 0; //build sum[]
for(int i=0; i<nums.length; i++){
if(i <= k-1){
total += nums[i];
}else{
total = total + nums[i] - nums[i-k];
}
if(i-k+1>=0){
sum[i-k+1] = total;
}
} int max = 0;
//build lef[]
for(int i=0; i<=nums.length-k; i++){ //i-k+1 < nums.length -> j < n-k+1
if(sum[i] > max){
max = sum[i];
lef[i] = max;
IndexL[i] = i;
}else{
lef[i] = lef[i-1];
IndexL[i] = IndexL[i-1];
}
}
max = 0;
//build rig[]
for(int i=nums.length-k; i>=0; i--){
if(sum[i] >= max){
max = sum[i];
rig[i] = max;
IndexR[i] = i;
}else{
rig[i] = rig[i+1];
IndexR[i] = IndexR[i+1];
}
}
// find 2rd subarray;
total = 0;
int ret = 0;
int[] ans = new int[3];
for(int i=k; i<=nums.length-2*k; i++){ // since no overlap so start with k;
total = sum[i] + lef[i-k] + rig[i+k]; //i+k <= nums.length-k
if(total > ret){
ret = total;
total = 0;
ans[0] = IndexL[i-k];
ans[1] = i;
ans[2] = IndexR[i+k];
}
}
return ans;
}
}
[leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和的更多相关文章
- [LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- Java实现 LeetCode 689 三个无重叠子数组的最大和(换方向筛选)
689. 三个无重叠子数组的最大和 给定数组 nums 由正整数组成,找到三个互不重叠的子数组的最大和. 每个子数组的长度为k,我们要使这3*k个项的和最大化. 返回每个区间起始索引的列表(索引从 0 ...
- [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- [Swift]LeetCode1031. 两个非重叠子数组的最大和 | 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 ...
- LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...
- [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- leetcode面试题42. 连续子数组的最大和
总结一道leetcode上的高频题,反反复复遇到了好多次,特别适合作为一道动态规划入门题,本文将详细的从读题开始,介绍解题思路. 题目描述示例动态规划分析代码结果 题目 面试题42. 连续子数 ...
- 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...
随机推荐
- [UE4]C++静态局部变量
void testFunc() { ; // this only runs ONCE, even on // subsequent calls to testFunc()! cout << ...
- Django中间件执行流程
中间件函数是 django 框架为我们预留的函数接口, 让我们可以干预请求和应答的过程 1. 获取浏览器端的IP地址: 使用 request.META[‘REMOTE_ADDR’] 2. 使用中间件 ...
- linux-centos6/7初始配置
关闭防火墙 chkconfig iptables off centos7下的命令为 systemctl stop firewalld.service #停止Firewall systemctl dis ...
- 通过yum升级gcc/g++至版本4.8.2
最近在坐一个日期处理的问题,需要安装sxtwl模块,但是gcc版本4.7死活也安装不上,最后测试到只能升级gcc版本到4.8才得以解决 [root@123 bin]# gcc -vUsing buil ...
- sql server自增列值的获取
IDENT_CURRENT(tbname) 是看表对象.所以没有受作用域限制. SCOPE_IDENTITY() 受作用域限制.同一个会话里面不同作用域也会有差异 @@IDENTITY 受会话限制 ...
- 24. (ora-01410无效的rowid)临时表 on commit delete rows 与 on commit preserve rows 的区别
ora-01410无效的rowid解决方式: 把临时表空间改成会话级别的就可以了,即把临时表的创建选项由on commit delete rows改为on commit preserve rows,就 ...
- myBatis连接MySQL报异常:No operations allowed after connection closed.Connection was implicitly closed
网站运行一个晚上,早上来上班,发现报错: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTra ...
- VBA 获取文件夹内的文件列表
Private strFiles(20) Private Sub GetFiles(ByVal strFolder As String, ByVal ty As String) ' 获取文件列表 If ...
- opencv一些资料
中文论坛: http://www.opencv.org.cn/forum.php?mod=forumdisplay&fid=1 yuv与mat转换: https://www.cnblogs.c ...
- 【OpenPose-Windows】运行OpenposeDemo.exe 如何保存图像运行结果及关节点信息
跑过很多次openposeDemo了,不管是video.Webcam还是Images,都是运行结果一闪而过,然而我们所要的肯定不是只看一下运行结果就完事的,很多情况下,我们都希望能够把运行结果的图像. ...