링크 : 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
분류 : 깊이/너비 우선 탐색 (DFS/BFS)
#include <string>
#include <vector>
using namespace std;
int answer = 0;
void dfs(vector<int> v, int target, int total, int num){
if(num==v.size()){
if(total==target) answer++;
return;
}
dfs(v,target,total+v[num], num+1);
dfs(v,target,total-v[num],num+1);
}
int solution(vector<int> numbers, int target) {
dfs(numbers,target,0,0);
return answer;
}
프로그래머스에서 코테를 진행하는 경우가 많으므로, 프로그래머스에서도 연습을 시작하게 되었다.
확실히 백준보다는 쉬운 느낌이 없잖아 있다.
'🖥️ CS > SW Expert 외의 Algorithms' 카테고리의 다른 글
(프로그래머스 C++) Lv2 카펫 (1) | 2020.10.14 |
---|---|
(프로그래머스 C++) Lv3 여행경로 (0) | 2020.10.14 |
(프로그래머스 C++) Lv2 문제 중 문자열 (1) (0) | 2020.10.13 |
(프로그래머스 C++) 단어 변환 (0) | 2020.10.03 |
(프로그래머스 C++) 네트워크 (0) | 2020.10.03 |