forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuttingTheRodProblem.java
More file actions
71 lines (55 loc) · 2.34 KB
/
CuttingTheRodProblem.java
File metadata and controls
71 lines (55 loc) · 2.34 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.geeksforgeeks.dynamicProgramming;
import com.geeksforgeeks.array.Rotate2DMatrix;
import java.util.ArrayList;
import java.util.List;
public class CuttingTheRodProblem {
public static void main(String[] args) {
getTheMaximumProfit(new int[]{2, 5, 9, 8}, 5);
getTheMaximumProfit(new int[]{1, 5, 8, 9, 10, 17, 17, 20}, 8);
getTheMaximumProfit(new int[]{3, 5, 8, 9, 10, 17, 17, 20}, 8);
}
public static int getTheMaximumProfit(int[] prices, int totalLength) {
int[][] priceMatrix = new int[prices.length][totalLength + 1];
for (int i = 1; i < priceMatrix.length; i++) {
for (int j = 1; j < priceMatrix[i].length; j++) {
if (i <= j) {
priceMatrix[i][j] = Math.max(prices[i - 1] + priceMatrix[i][j - i], priceMatrix[i - 1][j]);
} else {
priceMatrix[i][j] = priceMatrix[i - 1][j];
}
}
}
Rotate2DMatrix.print2DArray(priceMatrix);
System.out.println("Maximum Profit which can be achieved is " + priceMatrix[priceMatrix.length - 1][totalLength]);
getUsedPieces(priceMatrix);
return 0;
}
public static List<Integer> getUsedPieces(int[][] priceMatrix) {
List<Integer> usedPieces = new ArrayList<>();
int i = priceMatrix.length - 1;
for (int j = priceMatrix[0].length - 1; j > 0; ) {
if (priceMatrix[i][j] != priceMatrix[i - 1][j]) {
usedPieces.add(i);
j = j - i;
} else {
i--;
}
}
System.out.println("Used Pieces --->" + usedPieces);
return usedPieces;
}
public static void solveCuttingTheRodProblem(int[] piece, int[] price, int lengthOfRod) {
int[][] profit = new int[price.length + 1][lengthOfRod + 1];
// 1st row and 1st column is zero, Dummy Row and column
for (int i = 1; i < profit.length; i++) {
for (int j = 1; j < profit[i].length; j++) {
if (piece[i - 1] <= j) {
profit[i][j] = Math.max(price[i - 1] + profit[i][j - piece[i - 1]], profit[i - 1][j]);
} else {
profit[i][j] = profit[i - 1][j];
}
}
}
System.out.println(profit[profit.length - 1][lengthOfRod]);
}
}