쉬운 DFS문제이다.
이러한 수식 관련 DFS는 한번 풀어본 적 있기 때문에 쉽게 풀었다.
#include <string>
#include <vector>
using namespace std;
vector<int> num;
int cnt = 0;
void DFS(int cur, int ind, int target)
{
if (ind == num.size())
{
if (cur == target)
cnt++;
return;
}
DFS(cur + num[ind], ind + 1, target);
DFS(cur - num[ind], ind + 1, target);
}
int solution(vector<int> numbers, int target) {
int answer = 0;
num = numbers;
DFS(0, 0, target);
answer = cnt;
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 행렬 테두리 회전하기 (0) | 2022.05.19 |
|---|---|
| [프로그래머스] (C++) LV2 짝지어 제거하기 (0) | 2022.05.18 |
| [프로그래머스] (C++) LV2 더 맵게 (0) | 2022.05.16 |
| [프로그래머스] (C++) LV2 기능개발 (0) | 2022.04.28 |
| [프로그래머스] (C++) LV2 124 나라의 숫자 (0) | 2022.04.28 |