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 b6cf7ee commit 0c82a24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/inf/recursive,dfs/5_rere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

function solution(arr) {
let answer = "NO";
let flag = 0;
let total = arr.reduce((sum, x) => sum + x, 0);

function dfs(L, sum) {
if (flag) return;
if (L === arr.length) {
if (total - sum === sum) {
answer = "YES";
flag = 1;
}
} else {
// 포함하는 경우(왼쪽)
dfs(L + 1, sum + arr[L]); // 합에 해당 노드를 더해 넘기기
// 포함하지 않는 경우
dfs(L + 1, sum); // 합 넘기기
}
}
dfs(0, 0);
return answer;
}

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

0 comments on commit 0c82a24

Please sign in to comment.