Wood Cut
Description
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.
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
int l = 1 ,res = 0 , r = 0 ;
for(int item : L){
r = Math.max(r , item) ;
}
while(l <= r){
int mid = l + (r - l) /2 ;
if(count(L , mid) >= k){
res = mid ;
l = mid+1 ;
}else{
r = mid- 1;
}
}
return res ;
}
public int count(int[] L , int lon){
int sum = 0 ;
for(int item : L){
sum += item/lon ;
}
return sum ;
}
}