Skip to content

Commit 5ec25c1

Browse files
authored
docs(DP): update RodCutting.java
1 parent 6ff8732 commit 5ec25c1

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

Dynamic Programming/RodCutting.java

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
1-
/* A Dynamic Programming solution for Rod cutting problem
2-
Returns the best obtainable price for a rod of
3-
length n and price[] as prices of different pieces */
4-
1+
/**
2+
* A Dynamic Programming solution for Rod cutting problem
3+
* Returns the best obtainable price for a rod of
4+
* length n and price[] as prices of different pieces
5+
*
6+
*/
57
public class RodCutting {
6-
7-
private static int cutRod(int price[],int n)
8-
{
9-
int val[] = new int[n+1];
10-
val[0] = 0;
118

12-
for (int i = 1; i<=n; i++)
13-
{
14-
int max_val = Integer.MIN_VALUE;
15-
for (int j = 0; j < i; j++)
16-
max_val = Math.max(max_val,price[j] + val[i-j-1]);
17-
18-
val[i] = max_val;
19-
}
9+
private static int cutRod(int[] price, int n) {
10+
int val[] = new int[n + 1];
11+
val[0] = 0;
12+
13+
for (int i = 1; i <= n; i++) {
14+
int max_val = Integer.MIN_VALUE;
15+
for (int j = 0; j < i; j++)
16+
max_val = Math.max(max_val, price[j] + val[i - j - 1]);
17+
18+
val[i] = max_val;
19+
}
2020

21-
return val[n];
22-
}
21+
return val[n];
22+
}
2323

24-
//main function to test
25-
public static void main(String args[])
26-
{
27-
int arr[] = new int[] {2, 5, 13, 19, 20};
28-
int size = arr.length;
29-
System.out.println("Maximum Obtainable Value is " +
30-
cutRod(arr, size));
31-
}
24+
// main function to test
25+
public static void main(String args[]) {
26+
int[] arr = new int[]{2, 5, 13, 19, 20};
27+
int size = arr.length;
28+
System.out.println("Maximum Obtainable Value is " +
29+
cutRod(arr, size));
30+
}
3231
}

0 commit comments

Comments
 (0)