动态规划——Maximum Sum of 3 Non-Overlapping Subarrays
这个题对我来说真的是相当难的题目了,严格来讲可能不算是个动态规划的题目,但这个题目对类似的划分多个非重叠连续子区间的问题提供了一个很好解决方案
这个题目需要找三个非重叠的连续子区间,通过维护两个数组将第一个和第三个子区间可能的开始pos记录下来,在中间那个子区间开始的pos遍历时限制其边界范围,根据长度能恰到好处的将三个子区间划分成非重叠的,还使用了集合相减代替累加这样比较简单好用的技巧
下面提供代码:
class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int len = nums.length;
int[]sum = new int[len+1];
for(int i = 0;i<len;i++)
sum[i+1] = sum[i]+nums[i];
int[]posLeft = new int[len];
int[]posRight = new int[len];
posLeft[k-1] = 0;
int total = sum[k]-sum[0];
for(int i = k;i<len;i++) {
if(total<sum[i+1]-sum[i+1-k]) {
total = sum[i+1]-sum[i+1-k];
posLeft[i] = i-k+1;
}else posLeft[i] = posLeft[i-1];
}
posRight[len-k] = len-k;
total = sum[len]-sum[len-k];
for(int i = len-k-1;i>=0;i--) {
if(total<sum[i+k]-sum[i]) {
total = sum[i+k]-sum[i];
posRight[i]= i;
}else posRight[i] = posRight[i+1];
}
int l = 0,r = 0,max = 0;
int[]arr = new int[3];
for(int i = k;i<=len-2*k;i++) {
l = posLeft[i-1];
r = posRight[i+k];
total = (sum[l+k]-sum[l])+(sum[i+k]-sum[i])+(sum[r+k]-sum[r]);
if(total>max) {
arr[0] = l;arr[1] = i;arr[2] = r;
max = total;
}
}
return arr;
}
}
动态规划——Maximum Sum of 3 Non-Overlapping Subarrays的更多相关文章
- [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 ...
- [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三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
- 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 ...
- 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值
[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 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] 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】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
随机推荐
- Java 集合系列03之 ArrayList详细介绍
ArrayList做为List接口中最常用的实现类,必须掌握. 一.ArrayList简介 与Java中的数组相比ArrayList的容量可以动态增加.它继承与AbstractList,实现了List ...
- How to Create UML in Markdown
Import yuml class format  Create your own class Person ...
- python文本操作—读、写
文本文件存储的数据有很多,我们需要把这些文本里的内容读出来,然后在浏览器上面显示. 1.读取整个文本文件 格式: with open(路径) as 变量: 变量.read() 关键字with作用:在不 ...
- 最近面试被问到一个问题,AtomicInteger如何保证线程安全?
最近面试被问到一个问题,AtomicInteger如何保证线程安全?我查阅了资料 发现还可以引申到 乐观锁/悲观锁的概念,觉得值得一记. 众所周知,JDK提供了AtomicInteger保证对数字的操 ...
- MySQL保留字不能作为字段名使用
在设计MySQL字段的时候,无意中使用InOut这个名称作为字段名称,结果前端提交后就是没有写入数据库!但后端没有任何提示,跟踪mySQL日志,也没有留下痕迹,反复查,不得其解. 后来实在没有办法情况 ...
- tomcat去掉ContextPath
众所周知,项目打成war包直接放到webapps下启动tomcat后访问项目需要带上ContextPath,也就是war包的文件名,需要去除掉这玩意最简单的办法是将war包重命名为ROOT.war,为 ...
- java控制多线程同时写一个文件
最近出现一个需求,大体要做的就是控制多线程同时操作一个文件.当时第一个反应是不要用synchronized,太low了,然后我就使用了读写锁ReentrantReadWriteLock,然后写完静下来 ...
- AXI4 STREAM DATA FIFO
参考:http://www.xilinx.com/support/documentation/ip_documentation/axis_infrastructure_ip_suite/v1_1/pg ...
- HtmlWebpackPlugin用的html的ejs模板文件中如何使用条件判断
折腾: [已解决]给react-hot-boilerplate中的index.html换成用HtmlWebpackPlugin自动生成html 期间,已经有了思路了,但是不知道如何在ejs的html中 ...
- codeforces gym 101611C 重链剖分构造
给一棵树 要求在一个20*1e6的矩阵上放下这棵树,每个点的坐标都是整数且所有边都不相叉 题解 按照重链遍历,先给轻儿子坐标,然后沿着重儿子向下走即可 #include <bits/stdc++ ...