Skip to content

Instantly share code, notes, and snippets.

@xingmarc
Created March 27, 2017 06:21
Show Gist options
  • Save xingmarc/01b7b78b75f0cbde5ead61e92828bbf4 to your computer and use it in GitHub Desktop.
Save xingmarc/01b7b78b75f0cbde5ead61e92828bbf4 to your computer and use it in GitHub Desktop.
Find all coin combinations
// find all combination of coins up to an amount of money.
function findCoinCombination(money, coins) {
var solution = Arrays.from(coins, function() {
return 0;
})
function helper(moneyLeft, level, solution) {
if (level == solution.length - 1) {
solution[level] = moneyLeft;
console.log(solution)
return;
}
for (var i = 0; moneyLeft >= i * coins[level] ; i++) {
solution[level] = i;
helper(moneyLeft - i * coins[level], level + 1, solution);
}
}
findCoinCombination(money, 0, solution);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment