Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.
Note:
If n is the length of array, assume the following constraints are satisfied:
- 1 ≤ n ≤ 1000
- 1 ≤ m ≤ min (50, n )
Examples:
Input:
nums = [7,2,5,10,8]
m = 2
Output:
18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
Thinking: binary answer: if segments smaller space, close to n, the lowest sum is max (A [i]), if there is only one interval, then that sum [0 ~ n-1];
Then becomes between the lower bound and the upper bound binary search the solution space, if the average value is a number K, greater than the number K of the number of sections can not split into sections, it is greater than K m represents may further increase, smaller than m representatives, K too, should be a small point;
Note that the last judgment when the first judgment start, and then determine the end; there it is possible to sum burst, into long on it.
Time: Log (sum (Ai)) * n Space: The (N)
class Solution {
public int splitArray(int[] nums, int m) {
if(nums == null || nums.length == 0) {
return 0;
}
long start = 0, end = 0;
for(int i = 0; i < nums.length; i++) {
start = Math.max(start, nums[i]);
end += nums[i];
}
while(start + 1 < end) {
long mid = start + (end - start) / 2;
if(cansplit(nums, mid) > m) {
start = mid;
} else {
// cansplit(nums, mid) < m;
end = mid;
}
}
// first check start; then check end;
if(cansplit(nums, start) > m) {
return (int)end;
}
return (int)start;
}
private int cansplit(int[] nums, long sumlimit) {
int count = 0;
int i = 0;
long sum = 0;
while(i < nums.length) {
while(i < nums.length && sum + nums[i] <= sumlimit) {
sum += nums[i];
i++;
}
count++;
sum = 0;
}
return count;
}
}
Ideas 2: This problem can dp, DFS + memo cache
Leftsum prefixsum preceding paragraph may be determined, followed by a cut subproblems m - 1 segment;
Time: O (m * n ^ 2) Space: O (m * n); Dp can be extended to have a negative case, if all is positive, binary search or above predominates;
class Solution {
public int splitArray(int[] nums, int m) {
if(nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int[] prefixsum = new int[n+1];
prefixsum[0] = 0;
for(int i = 1; i <= n; i++) {
prefixsum[i] = prefixsum[i - 1] + nums[i - 1];
}
int[][] cache = new int[n][m+1];
return splitArrayHelper(0, m, cache, nums, prefixsum);
}
private int splitArrayHelper(int j, int m, int[][] cache, int[] nums,
int[] prefixsum) {
int n = nums.length;
if(m == 1) {
return prefixsum[n] - prefixsum[j];
}
if(cache[j][m] != 0) {
return cache[j][m];
}
int res = Integer.MAX_VALUE;
for(int k = j; k < n - 1; k++) {
// j ~k sum;
int leftsum = prefixsum[k+1] - prefixsum[j];
// k+1 ~ n sum;
int rightsum = splitArrayHelper(k+1, m - 1, cache, nums, prefixsum);
res = Math.min(res, Math.max(leftsum, rightsum));
}
cache[j][m] = res;
return res;
}
}