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