# performance – Find the least number of coins required that can make any change from 1 to 99 cents

## performance – Find the least number of coins required that can make any change from 1 to 99 cents

What you are looking for is Dynamic Programming.

You dont actually have to enumerate all the possible combinations for every possible values, because you can build it on top of previous answers.

You algorithm need to take 2 parameters:

• The list of possible coin values, here `[1, 5, 10, 25]`
• The range to cover, here `[1, 99]`

And the goal is to compute the minimal set of coins required for this range.

The simplest way is to proceed in a bottom-up fashion:

``````Range     Number of coins (in the minimal set)
1   5   10   25
[1,1]     1
[1,2]     2
[1,3]     3
[1,4]     4
[1,5]     5
[1,5]*    4   1             * two solutions here
[1,6]     4   1
[1,9]     4   1
[1,10]    5   1             * experience tells us its not the most viable one :p
[1,10]    4   2             * not so viable either
[1,10]    4   1   1
[1,11]    4   1   1
[1,19]    4   1   1
[1,20]    5   1   1         * not viable (in the long run)
[1,20]    4   2   1         * not viable (in the long run)
[1,20]    4   1   2
``````

It is somewhat easy, at each step we can proceed by adding at most one coin, we just need to know where. This boils down to the fact that the range `[x,y]` is included in `[x,y+1]` thus the minimal set for `[x,y+1]` should include the minimal set for `[x,y]`.

As you may have noticed though, sometimes there are indecisions, ie multiple sets have the same number of coins. In this case, it can only be decided later on which one should be discarded.

It should be possible to improve its running time, when noticing that adding a coin usually allows you to cover a far greater range that the one you added it for, I think.

For example, note that:

`````` [1,5]    4*1  1*5
[1,9]    4*1  1*5
``````

we add a nickel to cover `[1,5]` but this gives us up to `[1,9]` for free!

However, when dealing with outrageous input sets `[2,3,5,10,25]` to cover `[2,99]`, I am unsure as how to check quickly the range covered by the new set, or it would be actually more efficient.

You can very quickly find an upper bound.

Say, you take three quarters. Then you would only have to fill in the gaps 1-24, 26-49, 51-74, 76-99 with other coins.

Trivially, that would work with 2 dimes, 1 nickel, and 4 pennies.

So, 3 + 4 + 2 + 1 should be an upper bound for your number of coins, Whenever your brute-force algorithm goes above thta, you can instantly stop searching any deeper.

The rest of the search should perform fast enough for any purpose with dynamic programming.

(edit: fixed answer as per Gabes observation)

#### performance – Find the least number of coins required that can make any change from 1 to 99 cents

You need at least 4 pennies, since you want to get 4 as a change, and you can do that only with pennies.

It isnt optimal to have more than 4 pennies. Instead of 4+x pennies, you can have 4 pennies and x nickels – they span at least the same range.

So you have exactly 4 pennies.

You need at least 1 nickel, since you want to get 5 as a change.

It isnt optimal to have more than 1 nickel. Instead of 1+x nickels, you can have 1 nickel and x dimes – they span at least the same range.

So you have exactly 1 nickel.

You need at least 2 dimes, since you want to get 20.

This means you have 4 pennies, 1 nickel and at least 2 dimes.

If you had less than 10 coins, you would have less than 3 quarters. But then the maximal possible change you could get using all coins is 4 + 5 + 20 + 50 = 79, not enough.

This means you have at least 10 coins. Thomass answer shows that in fact if you have 4 pennies, 1 nickel, 2 dimes and 3 quarters, all is well.