말랑한 하루

[Algorithm] 순열 (Permutation) 본문

Algorithm

[Algorithm] 순열 (Permutation)

지수는말랑이 2022. 7. 18. 16:47
반응형

※ JavaScript

const getPermutations = function (arr, select) {
    const results = [];
    if (select === 1) return arr.map((el) => [el]);

    arr.forEach((fixed, index, origin) => {
      const rest = [...origin.slice(0, index), ...origin.slice(index + 1)];
      const permutations = getPermutations(rest, select - 1);
      const attached = permutations.map((el) => [fixed, ...el]);
      results.push(...attached);
    });

    return results;
}
반응형
Comments