🙃410 split array largest sum
https://leetcode.com/problems/split-array-largest-sum/
class Solution {
public int splitArray(int[] nums, int m) {
// Find the sum of all elements and the maximum element
int sum = 0;
int maxElement = Integer.MIN_VALUE;
for (int element : nums) {
sum += element;
maxElement = Math.max(maxElement, element);
}
// Define the left and right boundary of binary search
int left = maxElement;
int right = sum;
int minimumLargestSplitSum = 0;
while (left <= right) {
// Find the mid value
int maxSumAllowed = left + (right - left) / 2;
// Find the minimum splits. If splitsRequired is less than
// or equal to m move towards left i.e., smaller values
// 因为需要的最少split数少于m,所以满足条件,可能还有更小的sum
// 不需要有subarray精确等于maxSumAllowed,只需要小于等于它就行了,
// binary search的好处就是可以loose condition,直到我们搜索到最优的
if (minimumSubarraysRequired(nums, maxSumAllowed) <= m) {
right = maxSumAllowed - 1;
minimumLargestSplitSum = maxSumAllowed;
} else {
// Move towards right if splitsRequired is more than m
left = maxSumAllowed + 1;
}
}
return minimumLargestSplitSum;
}
private int minimumSubarraysRequired(int[] nums, int maxSumAllowed) {
int currentSum = 0;
int splitsRequired = 0;
//最少的split数就是每个subarray都尽可能接近maxSumAllowed的时候
for (int element : nums) {
// Add element only if the sum doesn't exceed maxSumAllowed
if (currentSum + element <= maxSumAllowed) {
currentSum += element;
} else {
// If the element addition makes sum more than maxSumAllowed
// Increment the splits required and reset sum
currentSum = element;
splitsRequired++;
}
}
// Return the number of subarrays, which is the number of splits + 1
return splitsRequired + 1;
}
}实在是不想写一遍了,记得把这道题重做一遍
Last updated