We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 8b18aea commit 92ce599Copy full SHA for 92ce599
1 file changed
Dynamic Programming/2D/Subsequences/RodCutting.java
@@ -0,0 +1,26 @@
1
+public class RodCutting {
2
+ public static int cutRod(int price[], int n) {
3
+ int[][] dp = new int[n][n+1];
4
+
5
+ for(int i = 0; i<n; i++){
6
+ dp[i][0] = 0;
7
+ }
8
9
+ for(int j = 0; j<n+1; j++){
10
+ dp[0][j] = price[0]*j;
11
12
13
+ for(int i = 1; i<n; i++){
14
+ for(int j = 1; j<n+1; j++){
15
+ if((i+1)<=j){
16
+ dp[i][j] = Math.max(dp[i-1][j], price[i]+dp[i][j-(i+1)]);
17
18
+ else{
19
+ dp[i][j] = dp[i-1][j];
20
21
22
23
24
+ return dp[n-1][n];
25
26
+}
0 commit comments