Given a list of denomination of coins, I need to find the minimum number of coins required to get a given value.
My approach using greedy algorithm,
Divide value by max denomination, take remainder value and divide by second maximum denomination and so on till be get required value.
But this approach fails for some cases.
I want to know
- Approach which works for all cases
- Why greedy approach fails?
Example where approach fails.
Coins Denomination (1,3,4,5)
Value Required 7
Using Greedy Approach
(7/5)=1 and +2 because 3 and 4 can't be used so we need to use 2 1's value coins. So total of 3 coins.
However optimal value is 4+3 ( 2 coins).