-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxLengthForCutting.cpp
49 lines (37 loc) · 983 Bytes
/
MaxLengthForCutting.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// https://www.geeksforgeeks.org/maximum-length-possible-by-cutting-n-given-woods-into-at-least-k-pieces/
#include <iostream>
#include <algorithm>
#include <vector>
bool isValid(std::vector<int>& wood, const int K, const int len)
{
int count = 0;
for (int i = 0; i < wood.size(); ++i) {
count += wood[i] / len;
}
return count >= K;
}
int findMaxLengthCut(std::vector<int>& wood, int K)
{
if (wood.size() == 0) {
return -1;
}
int left = 1;
int right = *max_element(wood.begin(), wood.end());
while (left <= right) {
int mid = left + (right - left) / 2;
if (isValid(wood, K, mid)) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
int main()
{
std::vector<int> wood {5, 9, 7, 10, 3, 8, 11, 6};
int K = 5;
int maxLength = findMaxLengthCut(wood, K);
std::cout << "Max length cut would be : " << maxLength << "\n";
return 0;
}