-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy path494. Target Sum.cpp
42 lines (40 loc) · 1.12 KB
/
494. Target Sum.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
// Backtracking
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
int count = 0;
backtrack(nums, S, 0, 0, count);
return count;
}
void backtrack(vector<int>& nums, int S, int sum, int pos, int& count){
if(pos == nums.size()){
if(sum == S) count++;
return;
}
backtrack(nums, S, sum + nums[pos], pos + 1, count);
backtrack(nums, S, sum - nums[pos], pos + 1, count);
}
};
// DP
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int S) {
int sum = 0;
for(auto x: nums) sum += x;
if(sum < S || -sum > S) return 0;
vector<int>cur(2 * sum + 1);
vector<int>next(2 * sum + 1);
cur[sum] = 1;
for(int i = 0; i < nums.size(); i++){
for(int j = 0; j < 2 * sum + 1; j++){
if(cur[j] != 0){
next[j + nums[i]] += cur[j];
next[j - nums[i]] += cur[j];
cur[j] = 0;
}
}
swap(cur, next);
}
return cur[sum + S];
}
};