Optimize permutations algorithm.

This commit is contained in:
Oleksii Trekhleb 2018-12-14 13:56:15 +02:00
parent d9946c1249
commit 59c6f4df13

View File

@ -14,13 +14,14 @@ export default function permutateWithRepetitions(
// Init permutations array. // Init permutations array.
const permutations = []; const permutations = [];
// Get smaller permutations.
const smallerPermutations = permutateWithRepetitions(
permutationOptions,
permutationLength - 1,
);
// Go through all options and join it to the smaller permutations. // Go through all options and join it to the smaller permutations.
permutationOptions.forEach((currentOption) => { permutationOptions.forEach((currentOption) => {
const smallerPermutations = permutateWithRepetitions(
permutationOptions,
permutationLength - 1,
);
smallerPermutations.forEach((smallerPermutation) => { smallerPermutations.forEach((smallerPermutation) => {
permutations.push([currentOption].concat(smallerPermutation)); permutations.push([currentOption].concat(smallerPermutation));
}); });