-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCuttingRod.java
48 lines (45 loc) · 1.4 KB
/
CuttingRod.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
44
45
46
47
48
public class CuttingRod {
public int maxValue(int price[]){
int max[] = new int[price.length+1];
for(int i=1; i <= price.length; i++){
for(int j=i; j <= price.length; j++){
max[j] = Math.max(max[j], max[j-i] + price[i-1]);
}
}
return max[price.length];
}
public int maxValue1(int price[]){
int max[] = new int[price.length+1];
for(int i=1; i <= price.length; i++){
max[i] = price[i-1];
}
for(int i=1 ; i <= price.length; i++){
for(int j=1; j < i ; j++){
max[i] = Math.max(max[i], max[i-j] + max[j]);
}
}
return max[price.length];
}
public int recursiveMaxValue(int price[],int len){
if(len <= 0){
return 0;
}
int maxValue = 0;
for(int i=0; i < len;i++){
int val = price[i] + recursiveMaxValue(price, len-i-1);
if(maxValue < val){
maxValue = val;
}
}
return maxValue;
}
public static void main(String args[]){
CuttingRod cr =new CuttingRod();
int[] price = {3,5,8,9,10,20,22,25};
long t1 = System.currentTimeMillis();
int r = cr.recursiveMaxValue(price,8);
long t2 = System.currentTimeMillis();
System.out.println(r);
System.out.println(t2 - t1);
}
}