-
Notifications
You must be signed in to change notification settings - Fork 0
/
wood-cut.cpp
52 lines (41 loc) · 1.03 KB
/
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
52
class Solution {
int count (vector<int> &L, int length) {
int c = 0;
if (length == 0) return c;
for (auto &i : L) {
c += i / length;
}
return c;
}
int max (vector<int> &L) {
if (L.size() == 0) return 0;
int max = L[0];
for (auto &i : L) {
if (max < i) max = i;
}
return max;
}
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) {
int start = 0;
int end = this->max(L);
int length = start;
while (start < end) {
int old = length;
length = start + (end - start) / 2;
if (old == length) break;
int _count = this->count(L, length);
if (_count >= k) {
start = length;
} else {
end = length;
}
}
return length;
}
};