Skip to content

Commit 92ce599

Browse files
Create RodCutting.java
1 parent 8b18aea commit 92ce599

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)