410. Split Array Largest Sum
https://leetcode.com/problems/split-array-largest-sum/
/*
* @lc app=leetcode id=410 lang=cpp
*
* [410] Split Array Largest Sum
*/
class Solution {
public:
int splitArray(vector<int>& nums, int m) {
const auto check = [](vector<int>& weights, long mid) {
long sum = 0, c = 1;
for (int w : weights) {
if (sum + w > mid) {
sum = 0;
++c;
}
sum += w;
}
return c;
};
long start = 1, end = 1;
for (int w : nums) {
start = max(start, (long) w);
end += w;
}
while (start < end) {
long mid = start + (end - start) / 2;
if (m < check(nums, mid)) {
start = mid + 1;
} else {
end = mid;
}
}
return start;
}
};
Last updated