Vending Machine Coin Mixes
An old station vending machine gives no change, so you must pay the exact price. A drink costs 30 cents and the machine takes 5, 10 and 25 cent coins: six 5s, three 10s, a 25 and a 5, and so on.
Order does not matter: a 25 then a 5 is the same handful as a 5 then a 25. You want the number of different handfuls.
Your task
Write countMixes(coins, price). coins lists the accepted coin values, and you have as many of each as you need. Return how many different handfuls add up to exactly price. Two handfuls are the same if they contain the same number of each coin value.
A price of 0 has exactly one handful: no coins at all. If the price cannot be paid, return 0.
countMixes(coins: int[], price: int) → int
Examples
coins = [5, 10, 25] price = 30
5
The handfuls are: six 5s; one 10 and four 5s; two 10s and two 5s; three 10s; one 25 and one 5. That is 5.
coins = [2, 3] price = 7
1
Only 2 + 2 + 3 works. Counting orders would give 3 (2-2-3, 2-3-2, 3-2-2), but those are all the same handful.
Constraints
- 1 ≤ coins.length ≤ 10
- 1 ≤ coins[i] ≤ 500, all values different
- 0 ≤ price ≤ 500
- The answer is below 10¹⁵.
Start with brute force
Recursively choose how many of each coin to use and count the choices that hit the price. With a 1-cent coin and many coin types, there can be trillions of handfuls, and the recursion visits every one.
The same subquestion, like "how many ways to pay 40 cents with only 5s and 10s", comes up again and again. A table answers each one once.
Hints
0 of 4 shown- Hint 1
In the second example, a careless count finds 3 answers for one handful by counting 2-2-3 and 3-2-2 separately. How can each handful be built in only one order?
- Hint 2
Decide coin types one at a time in a fixed order: how many 5s, then how many 10s, then how many 25s. Each handful then has one way to be built.
- Hint 3
Keep an array
ways[a]= number of handfuls that pay exactlyausing only the coin types handled so far. Start withways[0] = 1and zeros elsewhere. How does adding one new coin typecchange it? - Hint 4
For each coin type
c(outer loop), go through amountsafromcup toprice(inner loop):ways[a] += ways[a - c]. Going upward lets a handful use the new coin several times. Swapping the loops counts orders instead of handfuls.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The danger is counting one handful in several orders, so bring in coin types one by one. After each coin type, ways[a] counts handfuls for a made only from the types so far, and each handful is built exactly one way.
Trace coins [2, 3], price 7. Amounts 0 to 7:
- At the start the array is
[1, 0, 0, 0, 0, 0, 0, 0], because only a price of 0 can be paid, with no coins. - Add 2s. For a = 2..7,
ways[a] += ways[a - 2], which gives[1, 0, 1, 0, 1, 0, 1, 0]. Each even amount can be paid with 2s only, in exactly one way. - Add 3s. a = 3: += ways[0] → 1. a = 4: += ways[1] → 1. a = 5: += ways[2] → 1. a = 6: += ways[3], which is already 1 → 2. a = 7: += ways[4] → 1.
Result: ways[7] = 1 (2 + 2 + 3), and ways[6] = 2 (2 + 2 + 2 and 3 + 3). Going upward allowed 3 + 3: ways[3] already included one 3 when ways[6] read it.
function countMixes(coins, price) {
// ways[a] = handfuls that pay exactly a, using the coin types seen so far
const ways = new Array(price + 1).fill(0);
ways[0] = 1;
for (const c of coins) { // one coin type at a time
for (let a = c; a <= price; a++) {
ways[a] += ways[a - c]; // add one more coin c to a handful for a - c
}
}
return ways[price];
}def count_mixes(coins, price):
# ways[a] = handfuls that pay exactly a, using the coin types seen so far
ways = [1] + [0] * price
for c in coins: # one coin type at a time
for a in range(c, price + 1):
ways[a] += ways[a - c] # add one more coin c to a handful for a - c
return ways[price]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.