From 59c6f4df13775e8dd69c4e5f3244e7b2a91dd589 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 14 Dec 2018 13:56:15 +0200 Subject: [PATCH] Optimize permutations algorithm. --- .../sets/permutations/permutateWithRepetitions.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/algorithms/sets/permutations/permutateWithRepetitions.js b/src/algorithms/sets/permutations/permutateWithRepetitions.js index 90502d29..8c27bb2b 100644 --- a/src/algorithms/sets/permutations/permutateWithRepetitions.js +++ b/src/algorithms/sets/permutations/permutateWithRepetitions.js @@ -14,13 +14,14 @@ export default function permutateWithRepetitions( // Init permutations array. const permutations = []; + // Get smaller permutations. + const smallerPermutations = permutateWithRepetitions( + permutationOptions, + permutationLength - 1, + ); + // Go through all options and join it to the smaller permutations. permutationOptions.forEach((currentOption) => { - const smallerPermutations = permutateWithRepetitions( - permutationOptions, - permutationLength - 1, - ); - smallerPermutations.forEach((smallerPermutation) => { permutations.push([currentOption].concat(smallerPermutation)); });