-
Notifications
You must be signed in to change notification settings - Fork 0
/
RodCutting.java
54 lines (44 loc) · 1.27 KB
/
RodCutting.java
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
46
47
48
49
50
51
52
53
54
import java.util.*;
import javax.naming.ldap.Rdn;
public class Solution {
static int count(int index, int n, int price[], int[][] dp) {
if (index == 0) return n * price[0];
if (dp[index][n] != -1) return dp[index][n];
//operations
int notCut = 0 + count(index - 1, n, price, dp);
int cut = Integer.MIN_VALUE;
int rodLength = index + 1;
if (rodLength <= n)
cut = price[index] + count(index, n - rodLength, price, dp);
return dp[index][n] = Math.max(cut, notCut);
}
/**
* Tabulation
*/
static int count2(int price[], int n) {
int dp[][] = new int[n][n + 1];
//base case
for (int i = 0; i <= n; i++) dp[0][i] = i * price[0];
// represent states
for (int index = 1; index < n; index++) {
for (int len = 0; len <= n; len++) {
//operate
int notCut = 0 + dp[index - 1][len];
int cut = Integer.MIN_VALUE;
int rodLength = index + 1;
if (rodLength <= len)
cut = price[index] + dp[index][len - rodLength];
dp[index][len] = Math.max(notCut, cut);
}
}
return dp[n - 1][n];
}
public static int cutRod(int price[], int n) {
// Write your code here.
// int dp[][] = new int[n][n + 1];
// for (int i = 0; i < n; i++)
// Arrays.fill(dp[i], -1);
// return count(n - 1, n, price, dp);
return count2(price, n);
}
}