34 Knapsack Problem Final DP solution in Java
Rishi Srivastava Pseudo code: int dp new inttotalItems + 1capacity + 1; for (int i 1; i is less than or equal to totalItems; i++) for (int j 1; j is less than or equal to capacity; j++) if (weightsi 1 is less than or equal to j) dpij MAX(dpi 1j, valuesi 1 + dpi 1j weightsi 1); else dpij dpi 1j; return dptotalItemscapacity; Time complexity O(totalItems capacity) O(n2) Space complexity O(totalItems capacity) O(n2) Github: Hacker Earth:
|
|