-
Notifications
You must be signed in to change notification settings - Fork 22
/
183.wood-cut.cpp
51 lines (44 loc) · 1.07 KB
/
183.wood-cut.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
50
51
class Solution {
private:
bool isSufficient(vector<int> &L, int length, int nums)
{
if (length == 0)
{
return false;
}
int ans = 0;
for (auto i = 0; i < L.size(); ++i)
{
ans += L[i] / length;
}
return ans >= nums;
}
public:
/**
* @param L: Given n pieces of wood with length L[i]
* @param k: An integer
* @return: The maximum length of the small pieces
*/
int woodCut(vector<int> &L, int k) {
// write your code here
if (L.size() == 0)
{
return 0;
}
int start = 0;
int end = *max_element(L.begin(), L.end());
while(start + 1 < end)
{
int mid = start + (end - start) / 2;
if (isSufficient(L, mid, k))
{
start = mid;
}
else
{
end = mid;
}
}
return isSufficient(L, end, k) ? end : start;
}
};