-
Notifications
You must be signed in to change notification settings - Fork 0
/
CN Unbounded Knapsack.java
71 lines (53 loc) · 1.94 KB
/
CN Unbounded Knapsack.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Memoization
import java.util.Arrays;
public class Solution {
public static int unboundedKnapsack(int n, int w, int[] profit, int[] weight) {
// Write your code here.
int[][] dp = new int[n][w + 1];
for (int[] row: dp) Arrays.fill(row, -1);
return rec(n - 1, w, profit, weight, dp);
}
static int rec(int ind, int w, int[] profit, int[] weight, int[][] dp) {
if (ind == -1 || w == 0) return 0;
if (dp[ind][w] != -1) return dp[ind][w];
int notPick = rec(ind - 1, w, profit, weight, dp);
int pick = 0;
if (w >= weight[ind]) pick = profit[ind] + rec(ind, w - weight[ind], profit, weight, dp);
return dp[ind][w] = Math.max(pick, notPick);
}
}
// Tabulation
public class Solution {
public static int unboundedKnapsack(int n, int w, int[] profit, int[] weight) {
// Write your code here.
int[][] dp = new int[n + 1][w + 1];
for (int i = 1; i <= n; i++) {
for (int we = 1; we <= w; we++) {
int notPick = dp[i - 1][we];
int pick = 0;
if (we >= weight[i - 1]) pick = profit[i - 1] + dp[i][we - weight[i - 1]];
dp[i][we] = Math.max(pick, notPick);
}
}
return dp[n][w];
}
}
// Space optimization
public class Solution {
public static int unboundedKnapsack(int n, int w, int[] profit, int[] weight) {
// Write your code here.
int[][] dp = new int[n + 1][w + 1];
int[] prev = new int[w + 1];
for (int i = 1; i <= n; i++) {
int[] cur = new int[w + 1];
for (int we = 1; we <= w; we++) {
int notPick = prev[we];
int pick = 0;
if (we >= weight[i - 1]) pick = profit[i - 1] + cur[we - weight[i - 1]];
cur[we] = Math.max(pick, notPick);
}
prev = cur;
}
return prev[w];
}
}