조합 순열 JS

steyu·2022년 10월 4일
0
// 조합Combination (순서와 상관없음)
function findCombimation(arr, num, type) {
  if (num === 1) return arr.map((e) => [e]);
  const result = [];

  arr.forEach((fixed, index) => {
    const a = arr.slice(index + 1);
    const childComb = findCombimation(a, num - 1);
    childComb.map((e) => result.push([fixed, ...e]));
  });
  return result;
}


// 순열Permutation(순서고려);
function findPermutation(arr, num) {
  if (num === 1) return arr.map((e) => [e]);
  const result = [];

  arr.forEach((fixed, index) => {
    const a = [...arr];
    a.splice(index, 1);
    const childPermut = findPermutation(a, num - 1);
    childPermut.map((e) => result.push([fixed, ...e]));
  });
  return result;
}

0개의 댓글