반응형
https://programmers.co.kr/learn/courses/30/lessons/43165
코딩테스트 연습 - 타겟 넘버
n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다. -1+1+1+1+1 = 3 +1-1+1+1+
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int dfs(vector<int>& numbers, int idx, int sum, int target) {
if(idx == numbers.size()) {
if(sum == target) return 1;
return 0;
}
return dfs(numbers, idx + 1, sum + numbers[idx], target) + dfs(numbers, idx + 1, sum - numbers[idx], target);
}
int solution(vector<int> numbers, int target) {
return dfs(numbers, 0, 0, target);
}
반응형
'Algorithm' 카테고리의 다른 글
프로그래머스 : 베스트앨범 (0) | 2021.11.13 |
---|---|
프로그래머스 : 2 x n 타일링 (0) | 2021.11.13 |
프로그래머스 : 두 개 뽑아서 더하기 (0) | 2021.11.13 |
프로그래머스 : 순위 (0) | 2021.11.13 |
프로그래머스 : 정수 삼각형 (0) | 2021.11.13 |