-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path410.cpp
33 lines (33 loc) · 816 Bytes
/
410.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
// Runtime: 4 ms
// Memory Usage: 8.6 MB
public:
int splitArray(vector<int> &nums, int m) {
long long left = 0, right = 0;
for (auto num : nums) {
left = max(left, (long long)num);
right += num;
}
while (left < right) {
auto mid = left + (right - left) / 2;
if ([](const vector<int> &nums, int cuts, long long max) {
long long acc = 0;
for (auto num : nums) {
if (acc + num <= max)
acc += num;
else {
--cuts;
acc = num;
if (cuts < 0)
return false;
}
}
return true;
}(nums, m - 1, mid))
right = mid;
else
left = mid + 1;
}
return left;
}
};