Skip to content

Commit

Permalink
#8 22.11.10 > 부분집합 구하기
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Nov 10, 2022
1 parent 80f51dd commit b6cf7ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/inf/recursive,dfs/4_rere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";

function solution(n) {
let arr = [];
let checkArr = Array.from({ length: n + 1 }, () => 0);

// 부분집합 구하기
function dfs(v) {
if (v === n + 1) {
let value = "";
for (let i = 1; i <= n; i++) {
if (checkArr[i] === 1) value += i + " ";
}
if (value.length > 0) arr.push(value.trim());
} else {
checkArr[v] = 1; // 포함 시킴
dfs(v + 1); // 왼쪽
checkArr[v] = 0; // 포함시키지 않음
dfs(v + 1); // 오른쪽
}
}
dfs(1);
return arr;
}

console.log(solution(3));
2 changes: 1 addition & 1 deletion src/inf/recursive,dfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| 1 || 22.11.01 |
| 2 | O | 22.11.09 |
| 3 || 22.11.09 |
| 4 | X | O |
| 4 | X | 22.11.10😥 |
| 5 | X | O |
| 6 || O |
| 7 || O |
Expand Down

0 comments on commit b6cf7ee

Please sign in to comment.