반응형
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
- 백준
- TCP
- 크롤링
- typescript
- ip
- 알고리즘
- Nestjs
- HTTP
- 문자열
- 그래프
- 프로그래머스 레벨 2
- BFS
- 쉬운 문제
- socket
- Node.js
- 가천대
- 자바스크립트
- dfs
- Crawling
- javascript
- 타입 챌린지
- 수학
- HTTP 완벽 가이드
- type challenge
- 타입스크립트
- Algorithm
- 소켓
- 레벨 1
- dp
- 프로그래머스
Archives
- Today
- Total
kakasoo
[node.js] 타겟 넘버 ( 프로그래머스 레벨2 ) 본문
반응형
// 프로그래머스 레벨2 타겟넘버를 풀었습니다.
let num;
let visited;
let answer = 0;
const dfs = (plus, arr, cur, target) => {
visited[cur] = true;
if (plus) arr.push(num[cur]);
else arr.push(-num[cur]);
if (cur + 1 === num.length) {
if (arr.reduce((acc, cur) => acc + cur) === target) {
answer++;
}
return;
}
dfs(true, arr, cur + 1, target);
arr.pop();
visited[cur + 1] = false;
dfs(false, arr, cur + 1, target);
arr.pop();
visited[cur + 1] = false;
};
function solution(numbers, target) {
num = numbers;
visited = new Array(num.length).fill(false);
dfs(true, [], 0, target);
visited[0] = false;
dfs(false, [], 0, target);
return answer;
}
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] JadenCase ( 프로그래머스 레벨2 ) (0) | 2021.06.29 |
---|---|
[node.js] 카펫 ( 프로그래머스 레벨2 ) (0) | 2021.06.29 |
[node.js] 소수찾기 ( 프로그래머스 레벨2 ) (0) | 2021.06.29 |
[node.js] 가장 큰 수 ( 프로그래머스 레벨2 ) (0) | 2021.06.28 |
[node.js] 올바른 괄호 ( 프로그래머스 레벨2 ) (0) | 2021.06.28 |