Problem: Given coins with values , what is the minimum number of coins needed to obtain a cumulative value of cents out of these coins?
Coin Change
Suppose we have coins with values given as
- Can we make up the value of 7 with these coins? Yes, with coins !
- Can we make up the value of 25 with these coins? Yes, with coins !
- Can we make up the value of 23 with these coins? No.
While this problem is quite easy to solve with simple coin values such as , it easily becomes difficult with values we’re not used to (ex. )!
Solution (Kinda) 1: Greedy Approach
A rather intuitive approach to solving this problem is by using a greedy algorithm. If we want to minimize the number of coins used, then it would make sense to choose the largest coins to make up our sum, and work our way down to the smallest!
Thus, the greedy approach would attempt to solve the problem as follows. Given a cumulative coin value and a set of coins ,
- Choose the largest coin in the set of coins, . If , then subtract from as many times as needed until , and record the number of coins used.
- Choose the 2nd largest coin in the set of coins, . If , then subtract from as many times as needed until , and record the number of coins used.
- …
Consider the following examples.
Example: Coin Change (Greedy Approach)
Consider the set of coins
What is the minimum number of coins needed to obtain cents?
- We have 27 cents. Choose the largest coin, 10. We can use two 10-cent coins, to have 7 cents left.
- We have 7 cents. Choose the 2nd largest coin, 5. We can use one 5-cent coin to have 2 cents left.
- We have 2 cents. Choose the smallest coin, 1. We can use two 1-cent coins to have 0 cents left.
Thus, by greedy, we’ve found that our minimum number of coins is coins!
Unfortunately, greedy may not yield the correct answer in all cases. Consider the following:
Example: Limitations of the Greedy Approach
Suppose the set of coins
What is the minimum number of coins needed to obtain cents?
By the greedy approach, we would choose the coins (in this order) , giving us a “minimum” of 7. However, we know this isn’t the most optimal solution, as we can just choose four 10-cent coins instead ()!
Thus, in this case, the greedy approach does not yield the correct results.
Solution 2: Dynamic Programming
Recall that while the greedy algorithm may have seemed intuitive, it in fact fails in specific cases. Here, we discuss a solution to the problem that indeed does work in all cases, and uses dynamic programming.
Say we have the set of coins with values . Now suppose we have an array , where index of this array denotes the minimum number of coins needed to obtain coin value . Then, observe the following:
Say we wanted to find the next minimum coin count for , or in other words, . Interestingly enough, we can find this value as one of the following 3 possibilities:
- The minimum number of coins for , plus a 1-cent coin. In other words, .
- The minimum number of coins for , plus a 3-cent coin. In other words, .
- The minimum number of coins for , plus a 5-cent coin. In other words, .
We can generalize this finding. Say we have a set of coins with values , and we have the array where denotes the minimum number of coins needed to obtain value . Then, we can do the following.
- First, we let all elements in the array be , and set .
- Then, iterating through this array from left to right, we can find the value at as
For all indices , that exist.
Pseudocode for this algorithm is given below.
def min_coins(coins, n):
A = [sys.maxsize] * (n + 1) # sys.maxsize is a large number
A[0] = 0 # Base case, no coins needed at all
# Iterate, and populate all cells of A with minimum counts
for i in range(0, len(A)):
min_ways = sys.maxsize
for coin in coins: # Find min_ways
if 0 <= i - coin:
min_ways = min(min_ways, A[i - coin])
A[i] = min_ways
return A[n]
Analyzing the time complexity of this algorithm, we see that it takes time.