말랑한 하루
[Algorithm] 조합 (Combination) 본문
반응형
※ 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;
}
반응형
'Algorithm' 카테고리의 다른 글
[Algorithm] 정규 표현식 (Regular Expression) (0) | 2022.07.20 |
---|---|
[Algorithm] 순열 (Permutation) (0) | 2022.07.18 |
[Algorhtm] 후위 표기 식 (Postfix Expression) (0) | 2021.02.07 |
[Algorithm] 플로이드-와샬(Floyd-Warshall) (0) | 2020.12.18 |
[Algorithm] 세그먼트 트리 (Segment Tree) (0) | 2020.12.10 |
Comments