title: Knapsack Dynamic Programming Python
body: <p><strong>01 Knapsack problem:</strong> You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
  In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item, or don’t pick it (0-1 property).</p>
  <p>Link to the judge : <a href="https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1#" rel="nofollow noreferrer">LINK</a></p>
  <pre><code>Example 1:
  
  Input:
  N = 3
  W = 4
  values[] = {1,2,3}
  weight[] = {4,5,1}
  Output: 3
  Example 2:
  
  Input:
  N = 3
  W = 3
  values[] = {1,2,3}
  weight[] = {4,5,6}
  Output: 0
  </code></pre>
  <p><strong>My code:</strong></p>
  <pre><code># &lt;- Creating DP table -&gt;
  memory = []
  for x in range(1001):
      list1 = []
      for y in range(1001):
          list1.append(-1)
      memory.append(list1)
  
  class Solution:
      
      #Function to return max value that can be put in knapsack of capacity W.
      def knapSack(self,W, wt, val, n):
          
          if W == 0 or n &lt;= 0:
              return 0
          
          if memory[W][n] != -1:
              return memory[W][n]
          
          if wt[n-1] &gt; W:
              return self.knapSack(W,wt,val,n-1)
          
          memory[W][n] = max(val[n-1] + self.knapSack(W-wt[n-1],wt,val,n-1),self.knapSack(W,wt,val,n-1))
          
          return memory[W][n]
  </code></pre>
  <p><strong>Problem with the code:</strong>
  The code and the logic looks fine and I have been debugging it for sometime however some of the test cases are failing, whats wrong with the logic?</p>