Problem overview
Given a target amount and an IntArray of distinct coin values coins, change returns how many different combinations of coins add up to exactly amount. Each coin value can be used any number of times. A combination is defined by how many coins of each value it uses, not by their order, so 2 + 2 + 1 and 1 + 2 + 2 are the same combination.
For amount = 5 and coins = [1, 2, 5] the answer is 4: 5, 2 + 2 + 1, 2 + 1 + 1 + 1 and 1 + 1 + 1 + 1 + 1. For amount = 3 and coins = [2] the answer is 0, because 2s only make even sums. For amount = 0 the answer is 1: the combination that uses no coins at all.
Approach
Listing every combination is hopeless: with a few dozen coins and an amount of 5000, the count can pass a billion. The way out is to count instead of list.
Let ways[a] be the number of combinations that make the sum a, for every a from 0 to amount. The array starts with ways[0] = 1 (no coins make 0) and zeros elsewhere. The answer is ways[amount].
Now add the coins one at a time. When a coin is added, every combination for a - coin, plus one more of this coin, is a combination for a. So ways[a] grows by ways[a - coin]. Looping a upwards means ways[a - coin] may already include this coin, which is exactly how one coin value gets used many times.
Why the coins are the outer loop
This choice decides whether you count combinations or orderings. While a coin is being added, the counts so far use only the coins before it in the array. So each combination is built in one fixed order: all its coins of the first value, then all of the second, and so on. It is counted exactly once. 1 + 3 is counted when the 3 is added to a sum that already holds a 1, and 3 + 1 is never built.
Swap the loops (sums outside, coins inside) and any coin can be the last one added to any sum, so 1 + 3 and 3 + 1 are both counted. That counts orderings. With amount = 4 and coins = [1, 2, 3], the lesson's loop order returns 4 (1+1+1+1, 2+1+1, 2+2, 3+1) and the swapped version returns 7. With amount = 5 and coins = [1, 2, 5] the swapped version returns 9 instead of 4.
Step by step
- Make the table.
val ways = IntArray(amount + 1). A newIntArrayis filled with zeros, and the extra slot is for the sum 0.valkeeps the name on the same array while its entries still change. - Seed it.
ways[0] = 1. Every other count grows out of this one combination of no coins. - Loop over the coins. The outer
for (coin in coins)adds one coin value at a time. - Loop over the sums. The inner loop runs
aovercoin..amount. Sums belowcoincannot contain it. The range..includesamount, and whencoinis larger thanamountthe range is empty, so that coin does nothing. - The key line.
ways[a] += ways[a - coin]adds every combination fora - coin, extended by one coin. - Return
ways[amount].
The solution in Kotlin
class Solution {
fun change(amount: Int, coins: IntArray): Int {
val ways = IntArray(amount + 1)
ways[0] = 1
for (coin in coins) {
for (a in coin..amount) {
ways[a] += ways[a - coin]
}
}
return ways[amount]
}
}
Kotlin details worth knowing:
- Ranges are inclusive.
coin..amountincludes both ends. If you wrotecoin until amount, the last sum,amountitself, would never be filled. - Empty ranges are fine. When
coin > amount, the loop body never runs, so you do not need anif. Intwraps silently. A sum that passes 2147483647 wraps to a negative number with no error. The constraints promise that the answer and the count for every smaller amount fit in anInt, soIntArrayis safe here.ways[a] += ...works on array elements. It reads the element, adds, and writes it back.
Tracing an example
For amount = 5 and coins = [1, 2, 5], this is ways (indexes 0 to 5) at the start and after each coin:
start: [1, 0, 0, 0, 0, 0]
after coin 1: [1, 1, 1, 1, 1, 1]
after coin 2: [1, 1, 2, 2, 3, 3]
after coin 5: [1, 1, 2, 2, 3, 4]
With only 1s, each sum has one combination. Adding 2s gives sum 4 three combinations (1+1+1+1, 2+1+1, 2+2), and the coin 5 adds the combination 5 to sum 5, for 4 in total.
Complexity
Time: each of the n coins walks over at most amount + 1 sums, with constant work per sum: O(n · amount). With 300 coins and an amount of 5000, that is about 1.5 million steps.
Space: ways has amount + 1 entries: O(amount).
Edge cases and common mistakes
- Amount 0. The loops do nothing and the function returns
ways[0], which is 1.change(0, intArrayOf(7))returns 1. - Amount that cannot be made.
change(3, intArrayOf(5))returns 0, and so does the 3 with a single 2 coin. - Swapped loops. This is the classic mistake, and the tests with
amount = 4or5show it as 7 or 9 instead of 4. - Starting
ways[0]at 0. Then every entry stays 0, since all counts grow from that one seed. - Looping
adownwards. A downward loop lets each coin be used at most once, which solves a different problem.
Testing
The example inputs return these results:
change(5, [1, 2, 5]) = 4
change(3, [2]) = 0
change(4, [1, 2, 3]) = 4
change(0, [7]) = 1
change(3, [5]) = 0
