본문 바로가기
Coding Test/Programmers

[Programmers] (깊이/너비 우선 탐색(DFS/BFS)) Lv 2. 타겟 넘버

by song.ift 2024. 10. 29.

https://school.programmers.co.kr/learn/courses/30/lessons/43165

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

int answer = 0;

void dfs(const vector<int>& numbers, const int& target, int sum, int cnt)
{
    if (numbers.size() == cnt)
    {
        if (target == sum) ++answer;
        return;
    }

    dfs(numbers, target, sum - numbers[cnt], cnt + 1);
    dfs(numbers, target, sum + numbers[cnt], cnt + 1);
}

int solution(vector<int> numbers, int target) {
    //24.10.29
    dfs(numbers, target, 0, 0);
    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/967b2306cfc8e59442e3370f46d85ad5d4d8f3e1

 

[level 2] Title: 타겟 넘버, Time: 3.93 ms, Memory: 4.16 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@967b23

developeSHG committed Oct 29, 2024

github.com

 

댓글