Skip to content

Commit

Permalink
10.10: backtracking(부분수열의 합)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Oct 10, 2023
1 parent e533551 commit e3b5660
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions baekjoon/1182.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 부분수열의 합 : 백트래킹, 브루트 포스
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map((v) => parseInt(v, 10)));
const [n, s] = input.shift();
const arr = input.shift();
const visited = [];
let answer = 0;
let total = 0;

function isIncluded(num) {
for (let i = 0; i < visited.length; i++) if (visited[i] === num) return true;
return false;
}

function recur(depth = 0) {
if (total === s && depth > 0) answer++;
if (depth === n) return;

for (let i = 0; i < n; i++) {
if (isIncluded(i) || visited[visited.length - 1] >= i) continue;
visited.push(i);
total += arr[i];
recur(depth + 1);
visited.pop();
total -= arr[i];
}
}

recur();

console.log(answer);

0 comments on commit e3b5660

Please sign in to comment.