Skip to content

Instantly share code, notes, and snippets.

@israelalagbe
Created August 7, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save israelalagbe/97ff0b535a54684af4e17390af443f69 to your computer and use it in GitHub Desktop.
Save israelalagbe/97ff0b535a54684af4e17390af443f69 to your computer and use it in GitHub Desktop.
Coin change problem (algorithm)
function getWays(amount, coins) {
// Write your code here
const combinations = Array(amount+1).fill(0);
combinations[0] = 1
for(const coin of coins) {
for(let i = 1; i < combinations.length; i++) {
if(i >= coin) {
combinations[i] += combinations[i - coin];
}
}
}
return combinations[amount]
}
getWays(4, [1,2,3) // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment