coin-change.js
function coinChange(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let c = 0; c < coins.length; c++) {
if (coins[c] <= i) {
dp[i] = Math.min(dp[i], dp[i - coins[c]] + 1);
}
}
}
return dp[amount];
}
coinChange([1, 2, 5], 7);
What to watch
The dp array fills left to right; each cell takes the best of the previous sub-results.
Reading coin change (dp)on a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how coin change (dp) works.
Run coin change (dp) yourself
Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.
Open NeonFlow