Making change

The problem: how many ways can we make change for a given amount? We’ll solve this using dynamic programming. As we did with the 0/1 knapsack problem, we’ll build a table of intermediate solutions, bottom-up.

Consider the coins 1¢,2¢,5¢. Let’s make a table of the number of ways to make change for several amounts, given those weird coins. The table is as wide as the amount we’re interested in, and as tall as the number of coins:

                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?
    {1¢}   |?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?
  {1¢,2¢}  |?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?
{1¢,2¢,5¢} |?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?   ?
Well, there’s only one way to make change for 0¢: no coins:
                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |1
    {1¢}   |1   
  {1¢,2¢}  |1   
{1¢,2¢,5¢} |1   
Consider the first row, where we have no coins available. We simply cannot make change for any amount >0¢, so there are zero ways to make change for those amounts:
                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |1   0   0   0   0   0   0   0   0   0   0   0   0
    {1¢}   |1   
  {1¢,2¢}  |1   
{1¢,2¢,5¢} |1   
How about if a penny is available? There’s only one way to make 7¢ using pennies: seven pennies.
                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |1   0   0   0   0   0   0   0   0   0   0   0   0
    {1¢}   |1   1   1   1   1   1   1   1   1   1   1   1   1
  {1¢,2¢}  |1   
{1¢,2¢,5¢} |1   
Now, let’s throw 2¢ coins into the mix. For each cell in that row, we either use a 2¢ coin, or not. We will add the number of possibilities for each branch of that choice: What do we put in the cell? The sum of those two possibilities. (1)+(3)=[4]
                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |1   0   0   0   0   0   0   0   0   0   0   0   0
    {1¢}   |1   1   1   1   1   1  (1)  1   1   1   1   1   1
  {1¢,2¢}  |1   1   2   2  (3)  3  [4]  4   5   5   6   6   7
{1¢,2¢,5¢} |1   
Now, let’s add the row for the 5¢ coin. The values for 0¢…4¢ are the same, because we can’t use a nickel for those amounts. When we get to the 5¢ column, we add a possibility:
                                  amount 
            0¢  1¢  2¢  3¢  4¢  5¢  6¢  7¢  8¢  9¢ 10¢ 11¢ 12¢
            --------------------------------------------------
     ∅     |1   0   0   0   0   0   0   0   0   0   0   0   0
    {1¢}   |1   1   1   1   1   1   1   1   1   1   1   1   1
  {1¢,2¢}  |1   1   2   2   3   3   4   4   5   5   6   6   7
{1¢,2¢,5¢} |1   1   2   2   3   4   5   6   7   8  10  11  13