Skip to content

Instantly share code, notes, and snippets.

@statox
Created October 2, 2023 12:57
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 statox/0a6e2fb53fa1259b427da2e8a73a617a to your computer and use it in GitHub Desktop.
Save statox/0a6e2fb53fa1259b427da2e8a73a617a to your computer and use it in GitHub Desktop.
coins
const S = [1, 2, 3];
const n = 4;
const f = (S, n) => {
const stack = S.map((coin) => [coin]);
const permuts = new Set();
while (stack.length) {
const current = stack.shift();
const sum = current.reduce((s, c) => s + c);
if (sum > n) {
continue;
}
if (sum === n) {
console.log(current);
permuts.add(current.sort((a, b) => a - b).join(";"));
continue;
}
for (const c of S) {
stack.push([...current, c]);
}
}
return permuts.size;
};
console.log(f(S, n));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment