Problem overview
The method coinChange receives a vector coins of coin values and an integer amount. It returns the fewest coins that add up to exactly amount, or -1 when no combination does. Each coin value can be used as often as you like.
For coins = [1, 2, 5] and amount = 11 the answer is 3, from 5 + 5 + 1. For coins = [2] and amount = 3 the answer is -1, because 2s only make even sums. When amount is 0 the answer is 0: no coins are needed.
Approach: the best answer for every smaller sum
A tempting shortcut is to always take the largest coin that still fits. With coins = [1, 3, 4] and amount = 6 it picks 4, then 1, then 1: three coins, while 3 + 3 uses two. Greedy choices can fail here, so the solution compares every option instead.
Think about the last coin added to make a sum i. If that coin is coin, the coins before it make the sum i - coin, and the total is one more than the best for i - coin. Trying every coin as the last one and keeping the smallest result gives the best answer for i:
dp[i] = min(dp[i - coin] + 1) over all coins with coin <= i
The table dp is filled from the sum 0 upward, so every entry it needs is ready by then. This is dynamic programming: each answer reuses the answers for smaller sums.
For sums that can't be made, the table needs a marker. Every coin is worth at least 1, so no sum i ever needs more than i coins, and amount + 1 can never be a real answer. The table starts with that value everywhere, and an entry that keeps it at the end means "impossible".
Step by step
- Create
dpwithamount + 1elements, all equal toamount + 1. The constructor takes the size first and then the value of every element.dp[i]will be the fewest coins for the sumi. - Set
dp[0]to 0. The sum 0 takes no coins, and every other sum is built on top of it. - Loop over the sums
ifrom 1 toamountinclusive. Eachdp[i]needs only smaller sums, which are finished by then. - Loop over the coins with a range-based
for. Each coin is tried as the last one added to makei. - Check
coin <= i. A coin worth more thanican't be part of the sum, andi - coinwould be a negative index. - Update
dp[i]tomin(dp[i], dp[i - coin] + 1). Ifi - coincan't be made,dp[i - coin] + 1is already more thanamount, so it never looks like a real answer. - Return -1 if
dp[amount]is still greater thanamount: only an impossible sum keeps its starting value. - Otherwise return
dp[amount]. Whenamountis 0 the loops don't run and this returnsdp[0], which is 0.
The solution in C++
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + 1, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (coin <= i) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}
}
if (dp[amount] > amount) return -1;
return dp[amount];
}
};
For coins = [1, 3, 4] and amount = 6, the finished table is:
sum: 0 1 2 3 4 5 6
dp: 0 1 2 1 1 2 2
Sum 6 is reached from 3 (dp is 1) with a coin of 3, giving 2 coins. For coins = [2] and amount = 3 the table is:
sum: 0 1 2 3
dp: 0 4 1 4
The entries for 1 and 3 still hold the marker 4 (amount + 1), so the function returns -1.
The C++ details:
vector<int> dp(amount + 1, amount + 1)uses the constructor(count, value). Writing it with braces,{amount + 1, amount + 1}, would instead make a vector of two elements.for (int coin : coins)copies each value, which is cheap for anint.- Why the marker doesn't overflow.
dp[i - coin] + 1is at mostamount + 2, far below the 32-bit limit. UsingINT_MAXas the marker would overflow on the+ 1, and signed overflow is undefined behaviour in C++. - Huge coins are safe. A coin can be as large as 2^31 - 1. The check
coin <= iskips it before any subtraction, andiis at most 10,000.
Complexity
Time: O(n · amount), where n is the number of coins. There are amount sums, and each tries every coin with constant work.
Space: O(amount) for the table of amount + 1 integers.
Edge cases and common mistakes
amount = 0. The result is 0, withcoins = [1, 2, 5]or any other coins.- No combination.
coins = [5]withamount = 3returns -1: no coin fits, sodp[3]keeps its marker. - A coin larger than the amount. Without the
coin <= icheck,dp[i - coin]would read a negative index, which is undefined behaviour. - Greedy by the largest coin. For
[1, 3, 4]and 6, taking 4 first leads to 3 coins; the table finds 2. - A coin that skips some sums.
coins = [2, 5]withamount = 11returns 4 (2 + 2 + 2 + 5). The sums 1 and 3 are impossible, and the table handles that through their markers. - Returning
dp[amount]without the check. For an impossible amount it would return the marker,amount + 1, instead of -1.
Testing
coins = [1, 2, 5],amount = 11gives3.coins = [2],amount = 3gives-1.coins = [1, 3, 4],amount = 6gives2.coins = [1, 2, 5],amount = 0gives0.coins = [1, 3, 4],amount = 12gives3.coins = [2, 5],amount = 11gives4.coins = [2147483647, 1],amount = 10000gives10000.
