-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path494.cpp
More file actions
23 lines (22 loc) · 690 Bytes
/
494.cpp
File metadata and controls
23 lines (22 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int target) {
// left + right = sum
// left - right = target
// left = (sum + target) / 2
int sum = 0;
for (auto num : nums) sum += num;
if (abs(target) > sum) return 0;
if ((sum + target) & 1) return 0;
int newTarget = (sum + target) / 2;
vector<int> dp(newTarget + 1, 0);
dp[0] = 1;
for (int i = 0; i < nums.size(); i++) {
for (int j = newTarget; j >= 0; j--) {
if (j < nums[i]) continue;
dp[j] += dp[j - nums[i]];
}
}
return dp[newTarget];
}
};