-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCutRod.java
More file actions
54 lines (43 loc) · 1.01 KB
/
CutRod.java
File metadata and controls
54 lines (43 loc) · 1.01 KB
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
49
50
51
52
53
54
public class CutRod {
int findMaxPrice(int price[], int n) {
int table[] = new int[n + 1];
int k;
// Initializing the table to 0
for (k = 0; k < table.length; k++)
table[k] = 0;
//Base case
if (n <= 0)
return 0;
int i;
int maxValue = Integer.MIN_VALUE;
int temp, sum;
for (i = 0; i < price.length; i++) {
temp = 0;
sum = 0;
// Keeping track of incremental sum
for (int j = i; j < price.length; j++) {
temp = temp + j + 1;
sum = sum + price[j];
if (temp <= n) // Checking whether it has crossed the target
// size or not
{
maxValue = Math.max(maxValue, sum);
table[temp] = maxValue;
} else
break; // Break if bound are crossed
}
}
// Return the target value
return table[n];
}
public static void main(String[] args) {
CutRod ob = new CutRod();
int price[] = new int[5];
price[0] = 1;// 1
price[1] = 1;// 2
price[2] = 2;// 3
price[3] = 3;// 4
price[4] = 2;// 5
System.out.println(ob.findMaxPrice(price, 10));
}
}