Lintcode: Maximum Subarray III
Given an array of integers and a number k, find k non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note
The subarray should contain at least one number Example
Given [-1,4,-2,3,-2,3],k=2, return 8 Tags Expand
DP. d[i][j] means the maximum sum we can get by selecting j subarrays from the first i elements.
d[i][j] = max{d[p][j-1]+maxSubArrayindexrange(p,i-1)}, with p in the range j-1<=p<=i-1
public class Solution {
/**
* @param nums: A list of integers
* @param k: An integer denote to find k non-overlapping subarrays
* @return: An integer denote the sum of max k non-overlapping subarrays
*/
public int maxSubArray(ArrayList<Integer> nums, int k) {
// write your code
if (nums.size() < k) return 0;
int len = nums.size();
int[][] dp = new int[len+1][k+1];
for (int i=1; i<=len; i++) {
for (int j=1; j<=k; j++) {
if (i < j) {
dp[i][j] = 0;
continue;
}
dp[i][j] = Integer.MIN_VALUE;
for (int p=j-1; p<=i-1; p++) {
int local = nums.get(p);
int global = local;
for (int t=p+1; t<=i-1; t++) {
local = Math.max(local+nums.get(t), nums.get(t));
global = Math.max(local, global);
}
if (dp[i][j] < dp[p][j-1]+global) {
dp[i][j] = dp[p][j-1]+global;
}
}
}
}
return dp[len][k];
}
}
别人一个类似的方法,比我少一个loop,暂时没懂:
public class Solution {
/**
* @param nums: A list of integers
* @param k: An integer denote to find k non-overlapping subarrays
* @return: An integer denote the sum of max k non-overlapping subarrays
*/
public int maxSubArray(ArrayList<Integer> nums, int k) {
if (nums.size()<k) return 0;
int len = nums.size();
//d[i][j]: select j subarrays from the first i elements, the max sum we can get.
int[][] d = new int[len+1][k+1];
for (int i=0;i<=len;i++) d[i][0] = 0;
for (int j=1;j<=k;j++)
for (int i=j;i<=len;i++){
d[i][j] = Integer.MIN_VALUE;
//Initial value of endMax and max should be taken care very very carefully.
int endMax = 0;
int max = Integer.MIN_VALUE;
for (int p=i-1;p>=j-1;p--){
endMax = Math.max(nums.get(p), endMax+nums.get(p));
max = Math.max(endMax,max);
if (d[i][j]<d[p][j-1]+max)
d[i][j] = d[p][j-1]+max;
}
}
return d[len][k];
}
}
Lintcode: Maximum Subarray III的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Lintcode: Maximum Subarray Difference
Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...
- Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- LintCode: Maximum Subarray
1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
随机推荐
- 4Web镇之旅:开始链接
为了将网页发布到web上,需要一个全日工作的网络服务器,最好的方法是找到一家主机代理商. 域名是用来定位网站的第一无二的名字. 网页的最顶层目录就是根目录.在Web服务器中,因为根目录中的东西有可能在 ...
- Delphi 的绘图功能[1] - PolyBezier、PolyBezierTo
双击代码全选 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 unit Unit1; interface uses Windows, Messages, SysUtils ...
- MySQL安装图解教程
安装文件存放路径:不能有中文和空格! 校验 1 安装MySQL 2 校验MySQL 登录MySQL:mysql -uroot -p123 退出MySQL:exit | quit 查看数据库:show ...
- vimtutor
================================================================================ 欢 迎 阅 读 < V I M ...
- (IOS)Swift Music 程序分析
本文主要分享下楼主在学习Swift编程过程中,对GitHub上的一个开源App Swift Music的研究心得. 项目地址:https://github.com/xujiyao123/SwiftMu ...
- phpcms list页实现分页
{pc:content action="lists" catid="41" order="id ASC" num="1" ...
- 【Android测试】【第二节】性能——CPU时间片
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5143192.html 前言 第一节讲CPU的时候留下了一个 ...
- asp.net实现关闭当前网页
asp.net实现关闭当前网页功能:Response.Write("<script>window.close();</script>");// 会弹出询问是 ...
- 设计模式:观察者模式(Observer)
定 义:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象.这个主题对象在状态发生 变化时,会通知所有观察者对象,使他们能够自动更新自己. 结构图: 抽象主题类: abstract c ...
- css3 loading效果
file:///E:/zhangqiangWork/2014/SPDbank/index.html 参考该网站 http://tobiasahlin.com/spinkit/ 查看源代码把里面的dom ...