반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 소켓
- 그래프
- javascript
- 프로그래머스 레벨 2
- type challenge
- 타입 챌린지
- socket
- 쉬운 문제
- 프로그래머스
- 타입스크립트
- Algorithm
- TCP
- Nestjs
- HTTP
- Crawling
- 레벨 1
- dfs
- typescript
- 크롤링
- 알고리즘
- ip
- 가천대
- 수학
- BFS
- 백준
- 문자열
- dp
- 자바스크립트
- HTTP 완벽 가이드
- Node.js
Archives
- Today
- Total
kakasoo
[node.js] 부분 수열의 합( 백준 1182번 ) 본문
반응형
// 백준 1182번 부분 수열의 합을 풀었습니다.
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const combinations = function* (elements, selectNumber) {
for (let i = 0; i < elements.length; i++) {
if (selectNumber === 1) {
yield [elements[i]];
} else {
const fixed = elements[i];
const rest = elements.slice(i + 1);
for (const a of combinations(rest, selectNumber - 1)) {
yield [fixed, ...a];
}
}
}
};
const input = [];
rl.on("line", (line) => {
input.push(line);
}).on("close", () => {
const target = Number(input.splice(0, 1)[0].split(" ")[1]);
const numbers = input[0].split(" ").map(Number);
let count = 0;
for (let i = 1; i <= numbers.length; i++) {
for (const a of combinations(numbers, i)) {
const sum = a.reduce((acc, cur) => acc + cur);
if (sum === target) {
count++;
}
}
}
console.log(count);
});
C++ 로 풀 때는 역시나 dfs로 풀었었다. 그런데 이게 더 효율적이다.
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] N-Queen ( 백준 9663번 ) (0) | 2021.08.04 |
---|---|
[node.js] 부등호 ( 백준 2529번 ) (0) | 2021.08.03 |
[node.js] 스타트와 링크 ( 백준 14889번 ) (0) | 2021.08.01 |
[node.js] 연산자 끼워넣기 ( 백준 14888번 ) (0) | 2021.07.31 |
[node.js] 로또 ( 백준 6603번 ) (0) | 2021.07.31 |