Created
October 2, 2023 12:59
-
-
Save statox/24c056d61ca48812ad595076d87fb195 to your computer and use it in GitHub Desktop.
coins
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
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