-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRodCuttingProduct.java
80 lines (65 loc) · 2.14 KB
/
RodCuttingProduct.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* https://www.techiedelight.com/maximum-product-rod-cutting/
*/
import java.util.Random;
class RodCuttingProduct {
private static void checkInputs(int[] array, int length) {
if (length < 0 || array == null) {
throw new IllegalArgumentException();
}
}
private static int profitRec(int[] costArray, int n) {
if (n <= 0) {
return n + 1;
}
int maxProfit = Integer.MIN_VALUE;
for (int i = 1; i <= n; ++i) {
int cost = costArray[i - 1] * profitRec(costArray, n - i);
maxProfit = Math.max(maxProfit, cost);
}
return maxProfit;
}
public static int profitRecursive(int[] costArray, int length) {
checkInputs(costArray, length);
return profitRec(costArray, length);
}
public static int tabular(int[] costArray, int n) {
checkInputs(costArray, n);
int[] costTable = new int[n + 1];
for (int i = 1; i <= n; ++i) {
costTable[i] = costArray[i - 1];
}
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
costTable[i] = Math.max(costTable[i], costArray[j - 1] * costTable[i - j]);
}
}
return costTable[n];
}
}
class Driver {
private static int[] randomArray(int n, int max) {
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; ++i) {
array[i] = rand.nextInt(max);
}
return array;
}
private static void printArray(int[] array) {
for (int a : array) {
System.out.print(a + " ");
}
System.out.println();
}
public static void main(String[] args) {
int n = 6;
int max = 9;
int[] cost = randomArray(n, max);
Driver.printArray(cost);
int profit = RodCuttingProduct.profitRecursive(cost, cost.length);
System.out.println("Max profit: ");
System.out.println("Recursive : " + RodCuttingProduct.profitRecursive(cost, cost.length));
System.out.println("Tabluar : " + RodCuttingProduct.tabular(cost, cost.length));
}
}