I am trying to solve a classic dynamic programming coin-change problem with a twist. This is a homework question, I am not looking for full solutions, just for a few pointers to see what I am doing wrong.
Input:
- amount
xwe want to pay in coins for some value. - set
drepresenting number of available coins of denominations (1c, 5c, 10c, 25c, 100c, 200c)
Output:
- Minimum number of coins that need to exchange hands for the payment.
Assumptions:
- Cash register operator has unlimited supply of coins and knows how to give optimal change.
What I tried to do so far:
I am trying to build an array C, such that every element C[i], is the /minimum/ number of coins that exchange hands for the amount i.
C[0] = 0
For C[i], I calculate it either by taking minimum of all C[i-di] plus 1, for all available denominations of coins. I then remove the coin I picked from the available coins.
This approach seems to work in simple cases, but when I have, for example:
99 99 0 0 0 1 0
My approach fails, because it's "cheaper" to way $1 which will yield an exchange of 2 coins, than to pay exact 99 cents in cents (exchange of 99 coins).
Any pointers will be appreciated.