描述
给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大。每个子数组的数字在数组中的位置应该是连续的。
返回最大的和。
子数组最少包含一个数
样例
样例 1:
输入:
nums = [1,2,3]
k = 1
输出:
6
解释:
1 + 2 + 3 = 6
样例 2:
输入:
nums = [-1,4,-2,3,-2,3]
k = 2
输出:
8
解释:
4 + (3 + -2 + 3) = 8
代码解析
// 方法:划分类DP
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(int[] nums, int k) {
if (nums.length < k) {
return 0;
}
int len = nums.length;
int[][] globalMax = new int[k + 1][len + 1];
int[][] localMax = new int[k + 1][len + 1];
for (int i = 1; i <= k; i++) {
localMax[i][i-1] = Integer.MIN_VALUE;
//小于 i 的数组不能够partition
for (int j = i; j <= len; j++) {
localMax[i][j] = Math.max(localMax[i][j-1], globalMax[i - 1][j-1]) + nums[j-1];
if (j == i)
globalMax[i][j] = localMax[i][j];
else
globalMax[i][j] = Math.max(globalMax[i][j-1], localMax[i][j]);
}
}
return globalMax[k][len];
}
}