您的位置:首页 > 其它

Binary Search---Wood Cut

2016-04-03 18:36 465 查看
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.

For L=[232, 124, 456], k=7, return 114.

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) {
int max = 0;
for (int i = 0; i < L.length; i++) {
max = Math.max(max, L[i]);
}

// find the largest length that can cut more than k pieces of wood.
int start = 1, end = max;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (count(L, mid) >= k) {
start = mid;
} else {
end = mid;
}
}

if (count(L, end) >= k) {
return end;
}
if (count(L, start) >= k) {
return start;
}
return 0;
}

private int count(int[] L, int length) {
int sum = 0;
for (int i = 0; i < L.length; i++) {
sum += L[i] / length;
}
return sum;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: