Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The coin change problem

1Brute-force algorithm

The find_min_coins_brute_force function takes as parameters

  1. the set of possible coins, C

  2. the amount to change, N

and returns the minimum number of coins required to change N.

The function uses recursion to extensively check all combinations and select the optimal one.

The minimum number of coins is 9

2Dynamic programming algorithm

The find_min_coins_dynamic_programming takes as parameters

  1. the set of possible coins, C

  2. the amount to change, N

The function returns a list containing

  1. the minimum number of coins required to change N

  2. the dynamic programming table

  3. an optimal solution

The function uses the algorithm discussed in class, whereby a recursive relation is used to fill a table. The last entry of the table is the optimal solution, and then the table itself can be used to trace back the solution in terms of coins.

The minimum number of coins is 3

The dynamic programming table is

|  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
|  0 |  1 |  2 |  3 |  1 |  1 |  2 |  3 |  2 |  2 |  2 |  3 |  3 |  3 |  3 |  3 |

An optimal solution is: [5, 5, 5]

3A simple exercise

Find the time complexity of the two solutions in terms of the length of C and of the amount of money to change N. In order to do so, one option is to extend the following example

Brute force elapsed time: 6.19 s
Dynamic programming elapsed time: 1.19e-04 s