Coin Change
Problem
Section titled “Problem”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.
You may assume that you have an infinite number of each kind of coin.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Let changes[i] be the fewest coins that make amount i. The last coin used for amount i is some coins[j], and the rest must be an optimal way to make i - coins[j]. So changes[i] is one more than the best changes[i - coins[j]]. Building the table from 0 upwards means every smaller amount is ready when it is needed.
Algorithm
Section titled “Algorithm”- Create
changesof sizeamount + 1, filled withINT_MAX(unreachable), and setchanges[0] = 0. - For every amount
ifrom1toamount, and every coincoins[j] <= i, setchanges[i]to the minimum of itself andchanges[i - coins[j]] + 1. - If
changes[amount]is stillINT_MAX, return-1; otherwise return it.
The table uses long long, so adding 1 to INT_MAX cannot overflow, and an unreachable state stays above INT_MAX.
Complexity
Section titled “Complexity”- Time: — for each of the =
amountvalues, every one of the coins is tried. - Space: — for the
changestable.
Edge cases
Section titled “Edge cases”amount = 0: returns0, as no coins are needed.
class Solution { public: int coinChange(vector<int>& coins, int amount) { vector<long long> changes(amount + 1, INT_MAX); changes[0] = 0; for (int i = 1; i <= amount; i++) { for (int j = 0; j < coins.size(); j++) { if (coins[j] <= i) { changes[i] = min(changes[i], changes[i - coins[j]] + 1); } } } if (changes[amount] == INT_MAX) return -1; return changes[amount]; }};Give it a try first
Attempt the problem on LeetCode — the code will still be here.