Wood Cut
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.
Notice
You couldn't cut wood into float length.
Example For L=[232, 124, 456], k=7, return 114.
Solution: Binary search length from 1 to max length. If the total number of that kind of length equals to k,then we find the result.
public int woodCut(int[] L, int k) {
if (L == null || L.length == 0)
return 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < L.length; i++) {
max = Math.max(max, L[i]);
}
int start = 1, end = max;
while(start <= end) {
int mid = start + (end - start) / 2;
if(findK(mid, L) >= k) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return end;
}
public int findK(int n, int[] L) {
int sum = 0;
for(int i = 0; i < L.length; i++) {
sum += L[i] / n;
}
return sum;
}