[Programmers] 타겟 넘버

Time Lapse :20min 0sec

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <vector>
using namespace std;
int NUM[20],tot = 0,T, size;
//20:00
vector<int> DP(1048576,-1);
int dp(int idx,int bit_mask){
if(idx==size)
return 0;
if(DP[bit_mask]!=-1)
return DP[bit_mask];
if(bit_mask&1<<idx){
DP[bit_mask] = dp(idx+1,bit_mask^1<<idx) - 2*NUM[idx];
return DP[bit_mask];
}
return dp(idx+1,bit_mask);
}

int DFS(int idx,int bit_mask){
if(idx==size){
if(T == tot + dp(0,bit_mask))
return 1;
return 0;
}
return DFS(idx+1,bit_mask)+DFS(idx+1,bit_mask|1<<idx);
}

int solution(vector<int> numbers, int target) {
T = target; size = numbers.size();
for(int i=0;i<numbers.size();i++)
tot += NUM[i] = numbers[i];
return DFS(0,0);
}

/*
* 포인터 처리
#include <vector>
using namespace std;
vector<int> *NUM;
int *Target;
int answer;
void DFS(int sum,int n) {
if(n >= NUM->size()){
if(sum == *Target) answer++;
return;
}
DFS(sum , n+1);
DFS(sum - NUM->at(n)*2, n+1);
}
int solution(vector<int> numbers, int target) {
int total = 0;
for(int i=0;i<numbers.size();i++)
total += numbers[i];
NUM = &numbers;
Target = &target;
DFS(total , 1);
DFS(total-2*numbers[0], 1);
return answer;
}
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/targetNumber/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.