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. Have you met this question in a real interview? Yes
Example
For L=[232, 124, 456], k=7, return 114. Note
You couldn't cut wood into float length. Challenge
O(n log Len), where Len is the longest length of the wood.
注意是在[1,max(L)]里面找
public class Solution {
/**
*@param L: Given n pieces of wood with length L[i]
*@param k: An integer
*return: The maximum length of the small pieces.
*/
public int woodCut(int[] L, int k) {
// write your code here
if (L==null || L.length==0 || k<=0) return 0;
Arrays.sort(L);
int l=1, r=L[L.length-1];
while (l <= r) {
int m = l+(r-l)/2;
if (calc(L, m)>=k) l=m+1;
else r=m-1;
}
return r;
} public int calc(int[] L, int len) {
int res = 0;
for (int i=0; i<L.length; i++) {
res += L[i]/len;
}
return res;
}
}