From 0f3705d22bc438cae4d85f50ddbd6782f9f746d5 Mon Sep 17 00:00:00 2001 From: Shibam Nath <112779826+shibam120302@users.noreply.github.com> Date: Mon, 17 Jul 2023 08:20:34 +0530 Subject: [PATCH] Update 03 Knapsack Bottom up.cpp --- .../Dynamic Programming/03 Knapsack Bottom up.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Algorithms/Dynamic Programming/03 Knapsack Bottom up.cpp b/src/Algorithms/Dynamic Programming/03 Knapsack Bottom up.cpp index 5195573..85e7217 100644 --- a/src/Algorithms/Dynamic Programming/03 Knapsack Bottom up.cpp +++ b/src/Algorithms/Dynamic Programming/03 Knapsack Bottom up.cpp @@ -8,6 +8,12 @@ int Knapsack(int wt[], int val[], int W, int n) { for (int j = 0; j <= W; j++) { if (i == 0 || j == 0) // base case // filling 1st row and 1st column of the matrix with zero as per the base condition of the recursive solution t[i][j] = 0; + } + + +for (int i = 1; i <= n; i++) { + for (int j = 1; j <= W; j++) { + else if (wt[i - 1] <= j) { // current wt can fit in bag // this is for the choice diagram of the recursive solution int val1 = val[i - 1] + t[i - 1][j - wt[i - 1]]; // take current wt // and after taking weight substract the inserted weight from the final weight int val2 = t[i - 1][j]; // skip current wt