Created
May 7, 2018 13:12
-
-
Save alexzobi/823c771e5eb57074320f3621cf6bde74 to your computer and use it in GitHub Desktop.
coins - top-down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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