-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWoodCutSolution.java
45 lines (42 loc) · 1.11 KB
/
WoodCutSolution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class WoodCutSolution {
/**
*@param L: Given n pieces of wood with length L[i]
*@param k: An integer
*return: The maximum length of the small pieces.
*/
public static int woodCut(int[] L, int k) {
// write your code here
int max = L[0];
for(int i = 0;i < L.length;i++){
max = L[i] > max ? L[i] : max;
}
int i = 1;
int j = max;
while(i <= j){
int len = (j-i)/2+i;
int sum = 0;
for(int m = 0 ;m < L.length ; m++){
sum+= L[m]/len;
}
System.out.println(sum);
System.out.println(len);
if(sum == k){
i = len + 1;
break;
}
else if(sum > k){
i = len + 1;
}
else{
j = len - 1;
}
}
return i - 1;
}
public static void main(String args[]){
int[] A = {232,124,456};
int k = 7;
//Integer a = new Integer(1);
System.out.println(woodCut(A,k));
}
}