Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexzobi/823c771e5eb57074320f3621cf6bde74 to your computer and use it in GitHub Desktop.
Save alexzobi/823c771e5eb57074320f3621cf6bde74 to your computer and use it in GitHub Desktop.
coins - top-down
function change(amount, coins, idx, map = new Array(amount+1).fill(0)){
if(map[amount][idx] > 0){
return map[amount][idx];
}
if (idx >= coins.length - 1) return 1;
let coin = coins[idx];
let combos = 0;
for (let i=0; i* coin <= amount; i++){
let amountRemaining = amount - i * coin;
combos += change(amountRemaining, coins, idx + 1, map);
}
map[amount][idx] = combos;
return combos;
}
change(25,[25,10,5,1],0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment