-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0494.cpp
More file actions
37 lines (36 loc) · 1.08 KB
/
Copy path0494.cpp
File metadata and controls
37 lines (36 loc) · 1.08 KB
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
// Initial solution
class Solution {
public:
int findTargetSumWays(vector<int> &nums, int target) {
unordered_map<int, int> crnt;
crnt[0] = 1;
for (int i = 0; i < nums.size(); i++) {
unordered_map<int, int> next;
for (auto &p : crnt) {
next[p.first + nums[i]] += p.second;
next[p.first - nums[i]] += p.second;
}
crnt = next;
}
return crnt[target];
}
};
// Optimized using array;
class Solution {
public:
int findTargetSumWays(vector<int> &nums, int target) {
int total = accumulate(nums.begin(), nums.end(), 0);
vector<int> dp(2 * total + 1);
dp[total] = 1;
for (int i = 0; i < nums.size(); i++) {
vector<int> next(2 * total + 1);
for (int j = 0; j < dp.size(); j++) {
if (!dp[j]) continue;
next[j + nums[i]] += dp[j];
next[j - nums[i]] += dp[j];
}
dp = next;
}
return abs(target) > total ? 0 : dp[target + total];
}
};