forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutting-ribbons.cpp
More file actions
26 lines (24 loc) · 760 Bytes
/
cutting-ribbons.cpp
File metadata and controls
26 lines (24 loc) · 760 Bytes
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
// Time: O(nlogr), r is sum(ribbons)/k
// Space: O(1)
class Solution {
public:
int maxLength(vector<int>& ribbons, int k) {
int64_t left = 1, right = accumulate(cbegin(ribbons), cend(ribbons), 0LL) / k;
while (left <= right) {
int mid = left + (right - left) / 2;
if (!check(ribbons, k, mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
private:
bool check(const vector<int>& ribbons, int k, int s) {
return accumulate(cbegin(ribbons), cend(ribbons), 0LL,
[&s](auto total, auto x) {
return total + x / s;
}) >= k;
}
};