말랑한 하루

[Algorithm] 조합 (Combination) 본문

Algorithm

[Algorithm] 조합 (Combination)

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

※ JavaScript

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

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

 

반응형
Comments