Coin change

acoustic, folk, soulful, warm · 4:02

Listen on 93

Lyrics

[Verse 1]
Got a problem on my hands, need to make some change today
Customer wants forty-seven cents, what coins should I pay
Got quarters, dimes, and nickels, pennies in my stash
Need the minimum count to make the perfect cash
Start with greedy thinking, take the biggest first
Quarter takes twenty-five, that's how we disburse
Two quarters make fifty, but that's too much you see
So one quarter leaves twenty-two, now follow me

[Chorus]
Bottom up or top down, dynamic's the way
Memoization saves the day, no work thrown away
Coin change algorithm, minimum we seek
Greedy works for standard coins, but DP's technique
Store the solutions, build them up right
From zero to target, optimize the flight

[Verse 2]
Now I got twenty-two cents left, what's my next move
Dime takes ten away, leaving twelve to prove
Another dime makes two cents, add a nickel too
Three coins total used, that's the path we'll pursue
But wait there's other ways, need to check them all
Two dimes and two pennies, that's four coins that fall
The greedy method worked here, but it won't always do
When custom coin sets come, DP will see you through

[Chorus]
Bottom up or top down, dynamic's the way
Memoization saves the day, no work thrown away
Coin change algorithm, minimum we seek
Greedy works for standard coins, but DP's technique
Store the solutions, build them up right
From zero to target, optimize the flight

[Verse 3]
Dynamic programming table, index by amount
F of zero equals zero, that's the base we count
For every coin denomination, check if we can use
If coin's less than or equal, then we get to choose
F of amount equals minimum of current best
Or one plus F of amount minus coin, put it to test
Fill the table bottom up, from one to target sum
When the loop is finished, optimal answer comes

[Bridge]
Subproblems overlap, that's why we store
Optimal substructure, mathematical core
Time complexity's target times coins in your set
Space complexity's target, that's the memory debt

[Chorus]
Bottom up or top down, dynamic's the way
Memoization saves the day, no work thrown away
Coin change algorithm, minimum we seek
Greedy works for standard coins, but DP's technique
Store the solutions, build them up right
From zero to target, optimize the flight

[Outro]
When the cashier calls your name and the change ain't right
Use dynamic programming, make your algorithm tight
From pennies to dollars, every amount you'll solve
Minimum coin change, watch your skills evolve

← Matrix chain multiplication | Edit distance →