DrawCode Algorithm Visualizer • Dynamic Programming • Medium

Coin Change

Tags: DP, 1D, Unbounded, Combinatorics

1. One-Liner

Coin change uses DP over amount (and sometimes coin index) to either minimize coins to reach S or count ordered/unordered ways, with unbounded supply per denomination.


2. The Problem It Solves

You have coin values c_i and target S. Classic tasks: fewest coins (or -1 if impossible), or number of combinations—definitions differ on order sensitivity; fix the loop order accordingly.


3. The Core Idea

Min coins: dp[x] = min over coins of 1+dp[x-c]. Count ways (combinations): outer loop over coins, inner over amount so each combination is counted once—avoid double-counting permutations.


4. How It Works (Step-by-Step)

StepWhat Happens
1Init dp[0]=0 (min) or 1 (count)
2For each amount x, relax using transitions
3Handle impossible with or 0
4Return dp[S]

5. Dry Run Example

Coins {1,3,4}, S=6: min coins 2 as 3+3; ways (combinations) depend on loop design—be explicit in interviews.


6. Key Properties

PropertyValue
UnboundedReuse coins
Pseudo-polyIn S and n

7. Where It Is Used

DomainUse
FinanceDenomination breakdown
CPClassic DP

8. Interview Tips

Clarify min vs count, permutation vs combination, and greedy fails (counterexample: 1,3,4 vs amount 6).


9. Comparison with Other Algorithms

VariantTypical DP
0/1 itemsKnapsack—each once
UnboundedCoin change—reuse

10. Complexity

MetricValue
TimeO(n·S) typical
SpaceO(S)

Implementation Example (PYTHON)

def coin_change_min(coins: list[int], amount: int) -> int:
    INF = amount + 1
    dp = [INF] * (amount + 1)
    dp[0] = 0
    for x in range(1, amount + 1):
        for c in coins:
            if c <= x:
                dp[x] = min(dp[x], 1 + dp[x - c])
    return -1 if dp[amount] > amount else dp[amount]

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer