动态规划——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 ...
随机推荐
- JavaScript 修改 CSS 伪类属性
背景 有时候我们希望通过JS代码控制伪类属性, 确苦于对策 实际上可通过向document.head中添加style子元素来实现 演示 function css(style_text) { var s ...
- redis---------AOF文件异常导致的redis无法载入
AOF损坏时的对策1.若在写AOF文件时Server崩溃则可能导致AOF文件损坏而不能被Redis载入.可通过如下步骤修复: 创建一个AOF文件的备份: cp appendonly.aof appen ...
- sparkRDD相关操作
RDD(弹性分布式数据集).RDD以分区中的每一行进行分布式计算.父子依赖关系. 一.RDD创建操作 1)数据集合 Val data=Array(1, 2, 3, 4, 5, 6, 7, 8, 9) ...
- spring kafka生产、消费消息
参考网址: https://blog.csdn.net/lansetiankong12/article/details/54946641 1.新建Maven项目-KafkaMaven ->点击n ...
- react项目构建
1.react脚手架 npm install -g create-react-app create-react-app myproject 2.页面配置(bootcdn) <script src ...
- 查询sql 索引
SELECT indexname = a.name , tablename = c. name , indexcolumns = d .name , a .indidFROM sysindexes a ...
- Android logcat lines missing原因分析
当出现类似如下错误日志时: 2019-04-14 17:51:14.506 10189-10189/com.ss.android.ex.parent D/GGK: no WonderfulVideo ...
- vue 生命周期详解
- springboot2.1.3集成单节点elasticsearch6.4.0
本案例写了一个关于医生医院搜索的例子,包括求和模式下的打分(分值与相关性有关)搜索,单节点时切勿配置节点名称和节点ip.github地址:https://github.com/zhzhair/spri ...
- git知识总结-1.git基础之数据存储
1.前言 git包含四种对象文件: blob tree commit tag(目前没用到,暂时忽略) 2. git对象的关系 图 git三种对象关系 粗略一看,可以大致感觉出blob类似于文件 ...