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的更多相关文章

  1. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  2. Lintcode: Maximum Subarray Difference

    Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...

  3. Lintcode: Maximum Subarray II

    Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...

  4. LintCode: Maximum Subarray

    1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...

  5. Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum

    这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...

  6. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  7. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  8. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  9. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

随机推荐

  1. 性能监控工具nmon

    工具集: Nmon:性能数据收集分析工具 Nmon analyser:性能数据分析工具,excel文件 nmon概述:     nmon是收集AIX或Linux主机的性能数据并分析的工具,使用简单易用 ...

  2. spring mvc配置文件dispatcher-servlet.xml详解

    Spring的配置文档<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...

  3. ArcMap 标注、注记、图形文本

    标注.注记.图形文本 2016年8月10日10:29 ArcMap中怎样向地图添加文本,其中标注与注记是重点内容,此处对此进行总结. 参考链接: ①地图文本基本词汇: 什么是文本? ArcGIS 提供 ...

  4. 3D模型修改

    xnalara模型修改---增添(技术交流贴2) 其实很早就想做这个教程(流程)但有一种叫拖延症的东东捆了我半年~~于是这个帖子诞生与此,,希望对某些骚年有用... 送TA礼物     回复 举报|1 ...

  5. zepto源码--插入节点--学习笔记

    与生成width和height使用的方法类似,通过`after`, `prepend`, `before`, `append`,这四者之间的共性,生成对应的函数.并根据这四个函数,生成 `insert ...

  6. insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001

    insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...

  7. git warning解决方案

    1.warning: LF will be replaced by CRLF in xxxxx. 设置: git config core.autocrlf false

  8. mongodb 导出查询结果到文件

    编写mongo查询语句到 find.js db.xxx.find( {status:1,publisherId:0 , appDesc: {$in: [ /.*privacy .*/ ,/.*kika ...

  9. Selenium2学习-034-WebUI自动化实战实例-032-获取页面 body 大小

    获取 body 元素大小的方法,非常简单,直接上码,敬请参阅! /** * Get body size * * @author Aaron.ffp * @version V1.0.0: autoSel ...

  10. Asp.net MVC 批量删除数据

    ProductList视图 <div class="mid"> <div id="editInfo"> @using (Html.Beg ...