Video · + 1 min read
In the backpack problem, we have a backpack that can carry lbs. Furthermore, we have some items, each with a value and a weight. We want to find the biggest value we can carry in the backpack without exceeding the weight capacity.
A sample set of values/weights is in the table below:

To solve this problem, we use dynamic programming. The particular technique we will use is memoization (not to be confused with memoRization), which is keeping a table of already computed values so we don't have to compute something many times.
Let's say we want to solve the problem with items of values and weights , and our backpack holds lbs. The columns will be numbered , and in the th row of that column we will solve the backpack problem using just the first items of values and weights .
We should also add a dummy column before all the others numbered and a dummy row before all the others filled with just zeros since no items implies total maximum value is .
Now we iteratively compute the values in the th row. The th column will always be since the backpack can hold lbs. In column , we enter the maximum of the following two quantities: the entry in column , row or plus the entry in column , row .
This process essentially chooses at each step whether it would be optimal to carry that item or not.
