Created
October 3, 2023 02:41
-
-
Save zlrlo/bd016a65128dd6baeedef6f0601d93ec to your computer and use it in GitHub Desktop.
[JS] 조합 구현
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function combinations(arr, n) { | |
| // 1개만 뽑는다면 그대로 조합을 반환한다. 탈출 조건으로도 사용된다. | |
| if (n === 1) return arr.map((v) => [v]); | |
| const result = []; | |
| // 요소를 순환한다 | |
| arr.forEach((fixed, idx, arr) => { | |
| // 현재 index 이후 요소를 추출한다. | |
| // index번째는 선택된 요소 | |
| const rest = arr.slice(idx + 1); | |
| // 선택된 요소 이전 요소들을 제외하고 재귀 호출한다. | |
| const combis = combinations(rest, n - 1); | |
| // 선택된 요소와 재귀 호출을 통해 구한 조합을 합쳐준다. | |
| const combine = combis.map((v) => [fixed, ...v]); | |
| // 결과 값을 추가한다. | |
| result.push(...combine); | |
| }); | |
| // 결과 반화 | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment