Created
August 7, 2022 16:12
-
-
Save israelalagbe/97ff0b535a54684af4e17390af443f69 to your computer and use it in GitHub Desktop.
Coin change problem (algorithm)
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 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