# Wood Cut

LintCode-183.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.

Challenge

O(n log Len), where Len is the longest length of the wood.

``````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) {
if (L == null || L.length == 0 || k <= 0) {
return 0;
}

Arrays.sort(L);
int start = 1, end = L[L.length - 1];

while (start <= end) {
int middle = start + (end - start) / 2;

if (getPieces(L, middle) >= k) {
start = middle + 1;
} else {
end = middle - 1;
}
}

return end;
}

int getPieces(int[] L, int len) {
int count = 0;

for (int i : L) {
count += i / len;
}

return count;
}
}
``````

Hope this helps,
Michael