본문 바로가기
Coding Test/Programmers

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

by song.ift 2024. 12. 22.

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

const int dfs(const vector<int>& numbers, const int& target, int start, int sum)
{
    if (start == numbers.size())
    {
        if (sum == target) return 1;
        return 0;
    }
    
    return dfs(numbers, target, start + 1, sum + numbers[start])
            + dfs(numbers, target, start + 1, sum - numbers[start]);
}

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

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

 

[level 2] Title: 타겟 넘버, Time: 5.77 ms, Memory: 4.17 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@cdbf54

developeSHG committed Dec 22, 2024

github.com

 

댓글