Why taking the biggest coin first can cost more coins, and how a table finds the fewest.
About this comparison
Both lanes must pay 8 in as few coins as they can, with as many coins worth 5, 4 and 1 as they like. Greedy is amber, dynamic programming cyan; each strip has a cell per amount from 0 to 8.
Greedy takes the biggest coin that fits what is still owed. From 8 it takes a 5 and lands on 3, where only the 1 fits. It never asks whether another first coin would do better, so its cells stay amber: finished, not proved.
Dynamic programming (DP) answers every amount from 0 up, trying each coin that fits: spend it, plus the best answer already found for what is left. A cell turns green once all three coins are tried. Then DP walks back from 8 through the winning coins.
Greedy trusts each choice. DP keeps the best answer for every smaller amount: O(A × k) time and O(A) memory for amount A and k coin values, where greedy scans the coin values once per coin it pays. Greedy is quick; DP is always right.
Greedy's first coin is the mistake. The 5 leaves 3, which costs three 1s: 5 + 1 + 1 + 1, 4 coins. It jumps from 8 to 3 and never stands on 4, though one 4 pays it and two pay 8, so its cells 4 to 7 stay blank.
DP's strip ends 0 1 2 3 1 1 2 3 2. At 8 the 5 leaves 3, already 3 coins, so 4 in all; the 4 leaves 4, 1 coin, so 2 in all; the 1 cannot beat that. The walk back gives 4 + 4, half of greedy's purse. DP made 24 coin checks to greedy's 10, but the counters count coins paid, the question asked.
Use greedy when the coin system is canonical, so greedy gives the fewest coins for every amount: US, euro or rupee coins. A cashier is right to use it, and in an interview it is a fair first answer if you then show where it breaks.
Use DP for any other coin set, when you are not sure, or when some amounts cannot be made and you must say so. The same table counts the ways to make an amount (Coin Change II).