Published by Dimitris Kougioumtzis · Updated
Build minimum coin counts for every subtotal. Unlimited coins of each denomination.
For each amount, try each possible last coin and retain the smallest reachable coin count.
When to use Coin change (minimum coins)
Minimum-piece change with reusable denominations.
Choosing an alternative
Coin-change counting asks how many combinations exist instead.
Worked example: input and state changes
Example input
1, 3, 4 | 12
These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.
Step 0: Initial state.
State at this checkpoint
{ "values": [ "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞" ], "active": [] }Step 5: Subtotal 4: use coin 1, minimum 2.
State at this checkpoint
{ "values": [ 0, 1, 2, 1, 2, "∞", "∞", "∞", "∞", "∞", "∞", "∞", "∞" ], "active": [ 4 ] }Step 11: Subtotal 7: use coin 3, minimum 2.
State at this checkpoint
{ "values": [ 0, 1, 2, 1, 1, 2, 2, 2, "∞", "∞", "∞", "∞", "∞" ], "active": [ 7 ] }Step 16: Subtotal 10: use coin 3, minimum 3.
State at this checkpoint
{ "values": [ 0, 1, 2, 1, 1, 2, 2, 2, 2, 3, 3, "∞", "∞" ], "active": [ 10 ] }Step 21: Minimum coins: 3.
State at this checkpoint
{ "values": [ 0, 1, 2, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 ], "active": [] }
Expected final state
Minimum coins: 3.
Complexity and input bounds
Family: Dynamic programming. Time complexity: O(amount × coins).
Space complexity: O(amount) DP storage.
Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.
Positive coin denominations (1–24 values, 1–99), then | amount (1–40).
The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.
Common mistakes and limitations
A greedy largest-coin rule is not correct for arbitrary denominations; unreachable totals need a sentinel.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
1 2 3 4 5 6 7 8 9 10 11 12 | // a contains positive coin denominations; amount ≤ 40. Returns minimum coins, or -1. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation. function algorithm(a, n, amount) { let dp = Array(256).fill(0); for (let s = 1; s <= amount; s++) { dp[s] = 1000000; } for (let s = 1; s <= amount; s++) { for (let i = 0; i < n; i++) { if (a[i] <= s && dp[s - a[i]] + 1 < dp[s]) { dp[s] = dp[s - a[i]] + 1; } } } if (dp[amount] == 1000000) { return -1; } return dp[amount]; } |
Source
Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.
- The theory of dynamic programming
Richard Bellman (1954). Foundational method paper.