Wood Cut
http://www.lintcode.com/en/problem/wood-cut/#
Thoughts
Code
class Solution {
public:
int woodCut(vector<int> &L, int k) {
long start = 1, end = 0;
for (int l : L) {
end = max(end, (long)l);
}
++end;
const auto count = [&](int x) {
int res = 0;
for (const auto l : L) {
res += l / x;
}
return res;
};
while (start < end) {
long mid = start + (end - start) / 2;
if (count(mid) >= k) {
start = mid + 1;
} else {
end = mid;
}
}
return start - 1;
}
};
Analysis
Last updated