What is Coin Change Ways
Count combinations (order does not matter) that sum to amount. Loop coins on the outside so each coin is considered after the previous ones — 1+2 and 2+1 collapse into one combination.
Brute force approach
(index, remain): skip this coin, or spend one and stay. Counts combinations if the index never goes backwards.Combinations, not permutations: each call either takes the current coin again or moves to the next coin. The pair (coin index, remaining) repeats. Memoization stores that pair.
Amount up to 6 and at most 3 coins. 1 2 5 making 5 has 4 combinations.
Memoized approach
dp[x] += dp[x-c] so 1+2 and 2+1 stay one combination.Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.
1, 2, 5, amount 5. Four combinations: 5, 2+2+1, 2+1+1+1, 1×5. Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.Interactive DP trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each (coin, amount) | O(1) |
| Fill table | O(n · amount) |
| Space | O(amount) |
Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.
Implementations