-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation.js
More file actions
40 lines (34 loc) · 806 Bytes
/
permutation.js
File metadata and controls
40 lines (34 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
items: 선택한 요소를 담는 배열
k: list 중에서 선택하는 개수
**/
function permutation(items, list, k, result) {
if (items.length === k) {
result.push(items);
return;
}
for (let i = 0; i < list.length; i++) {
permutation(
[...items, list[i]],
list.filter((v, j) => j !== i),
k,
result
);
}
}
function permutationWithRepetition(items, list, k, result) {
if (items.length === k) {
result.push(items);
return;
}
for (let i = 0; i < list.length; i++) {
permutationWithRepetition([...items, list[i]], list, k, result);
}
}
const list = [1, 2, 3, 4];
let result1 = [];
permutation([], list, 2, result1);
console.log(result1);
let result2 = [];
permutationWithRepetition([], list, 2, result2);
console.log(result2);