forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodCutting.java
More file actions
45 lines (41 loc) · 1.85 KB
/
RodCutting.java
File metadata and controls
45 lines (41 loc) · 1.85 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
// This algorithm solves this problem:
// + "Given a rod of length n and an array that contains prices of all pieces of size
// smaller than n. Determine the maximum value obtainable by cutting up the rod and selling
// the pieces."
public class RodCutting
{
// This solution is correct but highly inefficient. Recursive calls aren't memoized
// so the poor code has to solve the same subproblem every time there's a single
// overlapping solution.
public int byRecursionApproach(int[] values, int length) {
if (length <= 0)
return 0;
int tmpMax = -1;
for (int i = 0; i < length; i++) {
tmpMax = Math.max(tmpMax, values[i] + byRecursionApproach(values, length - i - 1));
}
return tmpMax;
}
// With this approach we eliminate the need for recursive calls by solving the subproblems
// from the ground-up, utilizing the fact that all previous subproblems to a given problem
// are already solved. It follows the same basic principle from above, but adds memoization
// and excludes recursive calls.
public int byDynamicApproach(int[] values, int length) {
int[] subSolutions = new int[length + 1];
for (int i = 1; i <= length; i++) {
int tmpMax = -1;
for (int j = 0; j < i; j++)
tmpMax = Math.max(tmpMax, values[j] + subSolutions[i - j - 1]);
subSolutions[i] = tmpMax;
}
return subSolutions[length];
}
public static void main(String args[])
{
int arr[] = new int[] {1, 5, 8, 9, 10, 17, 17, 20};
int size = arr.length;
RodCutting rodCutting = new RodCutting();
System.out.println("Maximum obtainable value by recursion: " + rodCutting.byRecursionApproach(arr, size));
System.out.println("Maximum obtainable value by dynamic programming: " + rodCutting.byDynamicApproach(arr, size));
}
}