AiTechWorlds
AiTechWorlds
Watch the minimum-coins DP table build up for a target amount, with synced multi-language code.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| min coins | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
def coin_change(coins, amount):
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for a in range(1, amount + 1):
for c in coins:
if a - c >= 0:
dp[a] = min(dp[a], dp[a - c] + 1)
return -1 if dp[amount] == float('inf') else dp[amount]Coin Change computes the minimum number of coins needed to make a target amount, building up dp[a] = 1 + min over coins of dp[a − coin].
| Time | O(amount × coins) |
| Space | O(amount) |
The Coin Change Visualizer animates the dynamic programming solution for the minimum number of coins needed to make a target amount. dp[a] = 1 + min over each coin c of dp[a − c]. It runs in O(amount × coins) time and O(amount) space. Watch the table build up from dp[0] = 0.
Press Play
Watch dp[a] computed from smaller amounts for each coin.
Step through
Use Step Forward/Back or the timeline to follow each coin choice.
Read the code
View the Coin Change DP in C++, Java, Python, or JavaScript and copy it.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.