DSADPUnbounded

Coin Change Visualizer

What is Coin Change

Fewest coins that sum to amount, unlimited supply of each denomination. dp[x] is the fewest coins for sum x. Every coin c tries to improve dp[x] from dp[x-c] + 1.

  • Seed dp[0]=0, the rest a sentinel (amount+1).
  • Greedy fails on some systems (coins 1,3,4 and amount 6 wants two 3s, not 4+1+1).
  • Impossible: if the sentinel survives, return -1.

Brute force approach

For the remaining amount try every coin and recurse. Many remainders are reached by different sequences — O(n^{amount/min}).

Every coin is a branch from the remaining amount. The same remainder is expanded again and again. Memoization stores the fewest coins for each remainder.

Try a small input

Use at most 3 positive coins and an amount up to 7. 1 2 5 making 6 needs 2 coins (1+5).

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def coin(rem):
if rem == 0: return 0
best = min over coins of 1 + coin(rem-c)
Step 0

Memoized approach

dp[x] = 1 + min(dp[x-c]). Each amount is solved once. Time O(n · amount).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def coin(rem, memo):
if rem in memo: return memo[rem]
if rem == 0: return 0
memo[rem] = 1 + min coin(rem-c)
return memo[rem]
Step 0

How this visualizer works

Coins 1, 2, 5, amount 11. The table settles on 3 (5+5+1). 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 coinChange(coins, amount):
2dp = [0] + [amount+1]*amount
3for x in range(1, amount+1):
4for c in coins:
5if c <= x: dp[x] = min(dp[x], dp[x-c]+1)
6return dp[amount] if dp[amount] <= amount else -1

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

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

complexity.md

OperationTime
Each (amount, coin)O(1)
Fill tableO(amount · n)
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 coin_brute(coins, amount):
2if amount == 0:
3return 0
4if amount < 0:
5return -1
6best = -1
7for c in coins:
8sub = coin_brute(coins, amount - c)
9if sub >= 0 and (best < 0 or sub + 1 < best):
10best = sub + 1
11return best
1def coin_dp(coins, amount):
2inf = amount + 1
3dp = [0] + [inf] * amount
4for x in range(1, amount + 1):
5for c in coins:
6if c <= x and dp[x - c] + 1 < dp[x]:
7dp[x] = dp[x - c] + 1
8if dp[amount] >= inf:
9return -1
10return dp[amount]
1int coin_brute(int *coins, int n, int amount) {
2int best = -1;
3int i;
4int sub;
5if (amount == 0) {
6return 0;
7}
8if (amount < 0) {
9return -1;
10}
11for (i = 0; i < n; i++) {
12sub = coin_brute(coins, n, amount - coins[i]);
13if (sub >= 0 && (best < 0 || sub + 1 < best)) {
14best = sub + 1;
15}
16}
17return best;
18}
1int coin_dp(int *coins, int n, int amount) {
2int inf = amount + 1;
3int dp[10001];
4int x;
5int i;
6dp[0] = 0;
7for (x = 1; x <= amount; x++) {
8dp[x] = inf;
9for (i = 0; i < n; i++) {
10if (coins[i] <= x && dp[x - coins[i]] + 1 < dp[x]) {
11dp[x] = dp[x - coins[i]] + 1;
12}
13}
14}
15if (dp[amount] >= inf) {
16return -1;
17}
18return dp[amount];
19}