动态规划——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 ...
随机推荐
- numpy&pandas补充常用示例
Numpy [数组切片] In [115]: a = np.arange(12).reshape((3,4)) In [116]: a Out[116]: array([[ 0, 1, 2, 3], ...
- 记一次安装python umysql模块的报错
今天,在写一个python脚本的时候要用到数据库相关的umysql模块,但在引用的时候报没有此模块,第一反应就是去安装此模块,但是报没有找到pip命令. #pip install umysql -ba ...
- DB2读取CLOB字段-was报错:操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null
DB2读取CLOB字段-was报错:操作无效:已关闭 Lob. ERRORCODE=-4470, SQLSTATE=null 解决方法,在WAS中要用的数据源里面配置连个定制属性: progressi ...
- git add . 提示 `Changes not staged for commit`
- 解决axios在ie浏览器下提示promise未定义的问题
参考链接: https://blog.csdn.net/bhq1711617151/article/details/80266436 在做项目的时候发现在ie11上出现不兼容的问题,对于和后台交互这块 ...
- [转] C/C++ 调用Python
from : https://cyendra.github.io/2018/07/10/pythoncpp/ 目录 前言 官方文档 环境搭建 编译链接 Demo 解释器 初始化 GIL Object ...
- 利用C# 窗体设计 写一个抽奖游戏
老师布置了一个任务,要求我们做一个抽奖游戏,以下是我个人制作的一个作品与写项目的过程. 我们用到了8个pictureBox控件和一个button,设置好大小,并且编排成一个九宫个形状 添加窗体的背景图 ...
- 机器学习基石10-Logistic Regression
注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上一节课介绍了Linear Regression线性回归,用均方误差来寻找最佳 ...
- javascript闭包学习
(function(){})()===>>>>函数会被立即执行function(){}是一个函数用括号包起来表示是函数表达式再加()表示函数自执行 如何理解闭包?1.定义和用 ...
- .net 操作excel
.net 操作excel的常用组件:EPPlus,NPOI 1.NPOI,即POI的.NET版本(POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office文件, ...