-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximizeProfitForCuttingRod.java
More file actions
33 lines (30 loc) · 1.21 KB
/
MaximizeProfitForCuttingRod.java
File metadata and controls
33 lines (30 loc) · 1.21 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
package algorithm.dynamicprogramming;
/**
* Given a rod of length n and array P[i] for I = 1…..n, where P[i] is the price of rod of length i.
* Find the maximum revenue you can generate by cutting and selling the rod.
*
* </br></br>
* Formula = P[max] = Max(P[i] + R[n - i]) where i --> i to n and n --> 1 to length to find
*
* @author saukedia1
*
*/
public class MaximizeProfitForCuttingRod {
public static void main(String[] args) {
int rodLength = 5;
int[] priceArray = { 1, 5, 8, 9, 10 };
System.out.println(getMaximumProfit(rodLength, priceArray));
}
private static int getMaximumProfit(int rodLength, int[] priceArray) {
int[] maxPriceArray = new int[rodLength + 1];
maxPriceArray[0] = 0;
for (int tempRodLength = 1; tempRodLength <= rodLength; tempRodLength++) {
int maxPrice = -1;
for (int cutLength = 1; cutLength <= tempRodLength; cutLength++) {
maxPrice = Math.max(priceArray[cutLength - 1] + maxPriceArray[tempRodLength - cutLength], maxPrice);
}
maxPriceArray[tempRodLength] = maxPrice;
}
return maxPriceArray[priceArray.length];
}
}