DSADPCombinations

Coin Change Ways Visualizer

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.

  • dp[0] = 1 — one way to make nothing.
  • Outer coins, inner amounts = combinations. Swap them and you count permutations.
  • Unlimited: walking amount upward lets the same coin be reused.

Brute force approach

Recurse on (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.

Try a small input

Amount up to 6 and at most 3 coins. 1 2 5 making 5 has 4 combinations.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def ways(i, rem):
if rem == 0: return 1
return ways(i, rem-coin) + ways(i+1, rem)
Step 0

Memoized approach

Outer loop coins, inner loop amounts. 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.

Memoized recursion
def ways(i, rem, memo):
if (i, rem) in memo: return memo
if rem == 0: return 1
memo = take + skip
return memo
Step 0

How this visualizer works

Coins 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

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def change(coins, amount):
2dp = [1] + [0]*amount
3for c in coins:
4for x in range(c, amount+1):
5dp[x] += dp[x-c]
6return dp[amount]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def change(coins, amount):
2dp = [1] + [0]*amount
3for c in coins:
4for x in range(c, amount+1):
5dp[x] += dp[x-c]
6return dp[amount]

complexity.md

OperationTime
Each (coin, amount)O(1)
Fill tableO(n · amount)
SpaceO(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

dp.py

1def ways_brute(coins, amount, i=0):
2if amount == 0:
3return 1
4if amount < 0 or i == len(coins):
5return 0
6skip = ways_brute(coins, amount, i + 1)
7take = ways_brute(coins, amount - coins[i], i)
8return skip + take
1def ways_dp(coins, amount):
2dp = [1] + [0] * amount
3for c in coins:
4for x in range(c, amount + 1):
5dp[x] += dp[x - c]
6return dp[amount]
1int ways_brute(int *coins, int n, int amount, int i) {
2if (amount == 0) {
3return 1;
4}
5if (amount < 0 || i == n) {
6return 0;
7}
8return ways_brute(coins, n, amount, i + 1)
9+ ways_brute(coins, n, amount - coins[i], i);
10}
1int ways_dp(int *coins, int n, int amount) {
2int dp[5001];
3int i;
4int x;
5for (x = 0; x <= amount; x++) {
6dp[x] = 0;
7}
8dp[0] = 1;
9for (i = 0; i < n; i++) {
10for (x = coins[i]; x <= amount; x++) {
11dp[x] += dp[x - coins[i]];
12}
13}
14return dp[amount];
15}