rueki
프로그래머스- 타겟넘버(DFS) 본문
728x90
반응형
#include <string>
#include <vector>
using namespace std;
int cnt=0;
int DFS(int L, int s, vector<int> num,int t)
{
// 원소 개수
int n = num.size();
// 이진트리 깊이 == 원소개수 -> 재귀 멈추는 조건
if(L==n)
{
// total sum이 타겟값과 같다면?
if(s == t)
{
// 부분집합에 대한 갯수를 세는 것이기에 cnt++
cnt++;
}
}
else{
//종료조건이 안되면 계속 재귀 호출
DFS(L+1, s+num[L], num,t);
DFS(L+1, s-num[L],num,t);
}
return cnt;
}
int solution(vector<int> numbers, int target) {
int answer = DFS(0,0,numbers,target);
return answer;
}
728x90
반응형
'C, C++ 문제풀이' 카테고리의 다른 글
BOJ 9613. GCD 합 (0) | 2020.10.26 |
---|---|
BOJ 2609 최대공약수와 최소공배수 (0) | 2020.10.26 |
이진트리 순회 (0) | 2020.10.06 |
SW Expert Academy 8500. 극장 좌석 (0) | 2020.04.15 |
SW Expert Academy 8673. 코딩 토너먼트1 (0) | 2020.04.14 |
Comments