|
| 1 | +package DynamicProgramming.KnapsackProblem; |
| 2 | + |
| 3 | +// import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * * 0/1 Knapsack Problem |
| 7 | + */ |
| 8 | +public class Solution { |
| 9 | + public int knapsackProblem(int[][] items, int capacity) { |
| 10 | + // return knapsackProblemRec(items.length - 1, items, capacity); |
| 11 | + |
| 12 | + // int[][] cache = new int[items.length + 1][capacity + 1]; |
| 13 | + // for (int[] row : cache) Arrays.fill(row, Integer.MIN_VALUE); |
| 14 | + |
| 15 | + // return knapsackProblemMem(items.length, items, capacity, cache); |
| 16 | + |
| 17 | + return knapsackProblemDP(items, capacity); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * * Dynamic Programming Approach |
| 22 | + * |
| 23 | + * * TC: O(nc) |
| 24 | + * * SC: O(nc) |
| 25 | + */ |
| 26 | + private int knapsackProblemDP(int[][] items, int capacity) { |
| 27 | + int len = items.length; |
| 28 | + int[][] knapsack = new int[len + 1][capacity + 1]; |
| 29 | + |
| 30 | + for (int i = 0; i < len + 1; i++) { |
| 31 | + for (int j = 0; j < capacity + 1; j++) { |
| 32 | + if (i == 0 || j == 0) knapsack[i][j] = 0; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 1; i < len + 1; i++) { |
| 37 | + for (int j = 1; j < capacity + 1; j++) { |
| 38 | + int currentCapacity = j, valWithoutCurrentItem = knapsack[i - 1][currentCapacity]; |
| 39 | + |
| 40 | + if (items[i - 1][1] <= currentCapacity) { |
| 41 | + int valWithCurrentItem = |
| 42 | + items[i - 1][0] + knapsack[i - 1][currentCapacity - items[i - 1][1]]; |
| 43 | + knapsack[i][j] = Math.max(valWithCurrentItem, valWithoutCurrentItem); |
| 44 | + } else knapsack[i][j] = valWithoutCurrentItem; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return knapsack[len][capacity]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * * Memoization Approach |
| 53 | + * |
| 54 | + * * TC: O(nc) |
| 55 | + * * SC: O(nc) |
| 56 | + */ |
| 57 | + // private int knapsackProblemMem(int index, int[][] items, int capacity, int[][] cache) { |
| 58 | + // if (index == 0 || capacity == 0) return 0; |
| 59 | + |
| 60 | + // if (cache[index][capacity] != Integer.MIN_VALUE) return cache[index][capacity]; |
| 61 | + |
| 62 | + // if (items[index - 1][1] <= capacity) |
| 63 | + // return cache[index][capacity] = |
| 64 | + // Math.max( |
| 65 | + // items[index - 1][0] |
| 66 | + // + knapsackProblemMem(index - 1, items, capacity - items[index - 1][1], cache), |
| 67 | + // knapsackProblemMem(index - 1, items, capacity, cache)); |
| 68 | + |
| 69 | + // return cache[index][capacity] = knapsackProblemMem(index - 1, items, capacity, cache); |
| 70 | + // } |
| 71 | + |
| 72 | + /** |
| 73 | + * * Recursive Approach |
| 74 | + * |
| 75 | + * * TC: O(2^n) |
| 76 | + * * SC: O(2^n) |
| 77 | + */ |
| 78 | + // private int knapsackProblemRec(int index, int[][] items, int capacity) { |
| 79 | + // if (index == 0 || capacity == 0) return 0; |
| 80 | + |
| 81 | + // if (items[index][1] <= capacity) |
| 82 | + // return Math.max( |
| 83 | + // items[index][0] + knapsackProblemRec(index - 1, items, capacity - items[index][1]), |
| 84 | + // knapsackProblemRec(index - 1, items, capacity)); |
| 85 | + |
| 86 | + // return knapsackProblemRec(index - 1, items, capacity); |
| 87 | + // } |
| 88 | + |
| 89 | + public static void main(String[] args) { |
| 90 | + Solution solution = new Solution(); |
| 91 | + |
| 92 | + int[][] items = |
| 93 | + new int[][] { |
| 94 | + {60, 10}, |
| 95 | + {100, 20}, |
| 96 | + {120, 30}, |
| 97 | + }; |
| 98 | + |
| 99 | + // should be 220 |
| 100 | + System.out.println(solution.knapsackProblem(items, 50)); |
| 101 | + } |
| 102 | +} |
0 commit comments