动态规划——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 ...
随机推荐
- JGUI源码:实现日期控件显示(17)
本文实现一个日期控件显示,日期控件看起来很复杂,其实原理很简单,大部分情况下我们直接使用别人做得好的日期控件就行,但有时候特殊需求,比如显示提醒之类的,恐怕第三方控件就不好实现了, 为了使程序逻辑看起 ...
- elasticsearch 6.2.4添加用户密码认证
elasticsearch 6.3版本之前的添加认证需安装x-pack插件,6.3之后貌似去掉了这个. 1.安装x-pack 先切换到elastic用户下,在执行以下命令 $cd /data/elas ...
- Koa与Node.js开发实战(1)——Koa安装搭建(视频演示)
学习架构: 由于Koa2已经支持ES6及更高版本,包括支持async方法,所以请读者保证Node.js版本在7.6.0以上.如果需要在低于7.6的版本中应用Koa的async方法,建议使用Babel ...
- 【ShaderToy】基本操作——旋转
*继续:ShaderToy基础学习中d(`・∀・)b 对每个像素点进行旋转,其实加个公式就ok了啊. 对网格进行旋转: 代码如下: #define TUTORIAL 2 #define PI 3.14 ...
- 在Windows Server上安装ASP.NET时失败,提示not enough storage is available to process the command
今天在部署ASP.NET网站时出现IIS 500.21错误.环境是Windows Server 2012 +IIS8 于是查找解决方案,发现网上的信息都说是需要重装.NET framerwork4.0 ...
- Git首次配置
用户身份配置 安装好Git后的第一步是设置用户名和电子邮件地址.这一步是很重要的,因为以后这将作为你每一次提交的个人信息,写入所创建的提交中,不可更改. git config --global use ...
- Vue.js库的第一天的学习
一,vue.js简介 Vue.js可以作为一个js库来使用,也可以用它全套的工具来构建系统界面,这些可以根据项目的需要灵活选择 所以说, vue.js是一套构建用户界面的渐进式框架 Vue.js的核心 ...
- 【easy】532. K-diff Pairs in an Array
这道题给了我们一个含有重复数字的无序数组,还有一个整数k,让我们找出有多少对不重复的数对(i, j)使得i和j的差刚好为k.由于k有可能为0,而只有含有至少两个相同的数字才能形成数对,那么就是说我们需 ...
- Unity简单塔防游戏的开发——敌人移动路径的创建及移动
软件工程综合实践专题第一次作业 Unity呢是目前一款比较火热的三维.二维动画以及游戏的开发引擎,我也由于一些原因开始接触并喜爱上了这款开发引擎,下面呢是我在学习该引擎开发小项目时编写的一些代码的脚本 ...
- JavaScript代码规范
变量名:驼峰命名法(首单词小写,后面每个单词首字母大写) firstName = "John"; lastName = "Doe"; price = 19.90 ...