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[][] localMax = new int[k + 1][len + 1];
int[][] globalMax = new int[k + 1][len + 1];
for (int i = 1; i <= k; i++) {
localMax[i][i - 1] = Integer.MIN_VALUE;
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 (i == j) {
globalMax[i][j] = localMax[i][j];
} else {
globalMax[i][j] = Math.max(globalMax[i][j - 1], localMax[i][j]);
}
}
}
return globalMax[k][len];
}
}