-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnapsackProblem.java
43 lines (41 loc) · 1.09 KB
/
KnapsackProblem.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
import java.util.*;
class Program {
// O(nc) time | O(nc) space
public static List<List<Integer>> knapsackProblem(int[][] items, int capacity) {
// Write your code here.
int[][] dp = new int[items.length + 1][capacity + 1];
for (int i = 0; i < items.length; i++) {
int value = items[i][0];
int weight = items[i][1];
for (int c = 0; c <= capacity; c++) {
if (weight > c) {
dp[i + 1][c] = dp[i][c];
} else {
dp[i + 1][c] = Math.max(dp[i][c], dp[i][c - weight] + value);
}
}
}
return buildCombinedResult(dp, items, capacity, dp[items.length][capacity]);
}
private static List<List<Integer>> buildCombinedResult(int[][] dp, int[][] items, int capacity, int maxValue) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> values = List.of(maxValue);
result.add(values);
result.add(new ArrayList<>());
int i = items.length;
int c = capacity;
while (i > 0) {
if (dp[i][c] == dp[i - 1][c]) {
i--;
} else {
result.get(1).add(0, i - 1);
c -= items[i - 1][1];
i--;
}
if (c == 0) {
break;
}
}
return result;
}
}