Skip to content

Instantly share code, notes, and snippets.

@norbornen
Created November 1, 2019 10:58
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 norbornen/907ea7e14966bee96c3b686faba3f055 to your computer and use it in GitHub Desktop.
Save norbornen/907ea7e14966bee96c3b686faba3f055 to your computer and use it in GitHub Desktop.
function possibleSums(coins, quantity) {
let arr = new Set();
for (let i = 0, clen = coins.length; i < clen; i++) {
const coin = coins[i];
const qty = quantity[i];
const curr_sums = Array.from(arr);
const curr_coins = new Array(qty).fill().map((undefined, idx) => {
const n = coin * (idx + 1);
arr.add(n);
return n;
});
for (const s of curr_sums) {
for (let j = 0, jlen = curr_coins.length; j < jlen; j++) {
arr.add(curr_coins[j] + s);
}
}
}
return arr.size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment