Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Array Alchemist
LeetCode wizard with 75 conquered challenges under my belt.
Turning algorithm nightmares into "Aha!" moments, one explanation at a time.
Join me to level up your coding skills – from interviews to real-world application!
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
A real world example of this problem is a cashier working in the gas station, who want to give out minimum of the coins as possible.
Let's brainstorm a logic how we can solve it?
we have to make up to a certain amount.
For example:
let's say we have to give 11$. let's see how we can return 11$ in minimum coin?
we have 1, 2 and 5 as an option
we have following option
1 $ coin * 11(times) = 11$, => total 11 coins
2 $ coin * 5(times) = 10 + 1 coin = 11$, => total 6 coins
5 $ coin * 2(times) = 10 + 1$ coin = 11 $, => total 3 coins
thus, we'd return only 3 coins of two 5$ coins and one 1$ coin.
let's see what was the thinking process behind it?
These two things are certain. Let's see how we can build from it?
from number 1 point:
from 2:
We need to iterate and weigh all of our coins and compute the minimum coins to return
now let's see how we can combine these two:
Now,
let's use this example again:
let's have a variable, which will store the minimum amount of coins needed to return the amount A, in the position a.
let's assume, we first need to return 0
amount 0 1 2 3 4 5 6 7 8 9 10
let minmumCoinArray = [0, 0, 0, 0, 0 , 0 , 0, 0, 0, 0, 0 ]
let's see here
Easy to understand break down
Imagine you have a friend, and you ask them:
"Hey, if you had to pay 7 units with denominations 1, 2, and 5, what's the least number of coins you'd use?"
Day 1 - Your friend only remembers 1-unit coins:
To pay 1 unit, they'd use 1 coin of 1 unit.
To pay 2 unit, they'd use 2 coin of 1 unit
To pay 3 unit, they'd use 3 coin of 1 unit
To pay 7 unit, they'd use 7 coin of 1 unit
Your friend records this on a piece of paper:
Amount Coins Needed (just using 1s)
1 1 coins
2 2 coins
3 3 coins
4 4 coins
5 5 coins
7 7 coins
Day 2 - Your friend recalls the existence of the 2-unit coin:
Now, they think, "What if I use the 2-unit coin for each amount?"
dp[each amount i,e i]
Amount Coins Needed (just using 1s)
1 1 coins
2 1 coins ====> (1 + 0 )coins
3 3, (2 + 1 ) = 2 coins ====> (1 + 1 )coins
4 4, (2 + 2) = 2 coins ====> (1 + 1 )coins
5 5, ( 2 + 2 + 1) = 3 coins ====> (1 + 2 )coins
7 7, ( 2 + 2 + 2 + 1) = 4 coins ====> (1 + 3 )coins
Day 3 - Your friend recalls the 5-unit coin:
This time, they compare the results using the 5-unit coin against the previous best results.
dp[each amount i,e i]
Amount Coins Needed (just using 1s)
1 1
2 1
3 3, (2 + 1 ) = 3 coins
4 4, (2 + 2) = 2 coins
5 5, instead of using 3 coins, we will only use 1 coin of 5 units
7 7, instead of using 4 coins, we will only use 2 coins of 5 units + 1 coin of 2 units
here we are updating our memory with new calculation so this is sort of dynamic programming.
What't the recurrence relation?
For each amount, the way we are deriving the relation is:
whatever is the minimum of either
1. previous result i.e dp[i]
2. current coin + best way of paying the rest of the amount
i.e 1 + dp[i - coin]
dp[given amount] = Math.min(dp[given amount], 1 + dp[given amount - coin])
So here we know:
1. We need to iterate over the coins
2. We need to iterate over the amounts
so the actual code is
What are the complexities of this code?
Time Complexity = O(M * Amount), where M is total number of different coins
Space Complexity = O(Amount)
after submitting to Leetcode: