프로그래머스 타겟 넘버
2019. 9. 21. 18:13ㆍ알고리즘/프로그래머스
경우의 수는 +, -로 N제한이 20이므로 2^20의 시간복잡도는 1초 안에 충분히 해결할 수 있는 문제다. DFS를 통한 완전탐색으로 해결했다
문제: https://programmers.co.kr/learn/courses/30/lessons/43165
깃허브주소: https://github.com/surinoel/boj/blob/master/Programmers_타겟넘버.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
void go(int idx, int sum, const vector<int> &numbers, int &cnt, int target) { | |
if(idx == numbers.size()) { | |
if(sum == target) { | |
cnt += 1; | |
} | |
return; | |
} | |
go(idx + 1, sum + numbers[idx], numbers, cnt, target); | |
go(idx + 1, sum - numbers[idx], numbers, cnt, target); | |
} | |
int solution(vector<int> numbers, int target) { | |
int answer = 0; | |
go(0, 0, numbers, answer, target); | |
return answer; | |
} |
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 단어 변환 (0) | 2019.09.21 |
---|---|
프로그래머스 네트워크 (0) | 2019.09.21 |
프로그래머스 H-Index (0) | 2019.09.21 |
프로그래머스 가장 큰 수 (0) | 2019.09.21 |
프로그래머스 더 맵게 (0) | 2019.09.21 |