Skip to content

Commit 9c37192

Browse files
authored
Create 494. Target Sum.cpp
1 parent a1f076e commit 9c37192

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

494. Target Sum.cpp

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
//*************************** Approach 1 *****************
2+
class Solution {
3+
public:
4+
int solve(vector<int>& nums, int &target, int i, int sum, unordered_map<string, int>& memo) {
5+
if (i == nums.size()) {
6+
return sum == target ? 1 : 0;
7+
}
8+
9+
// Create a unique key for the current state
10+
string key = to_string(i) + "," + to_string(sum);
11+
12+
// Check if the result is already computed
13+
if (memo.find(key) != memo.end()) {
14+
return memo[key];
15+
}
16+
17+
// Compute the result recursively
18+
int plus = solve(nums, target, i + 1, sum + nums[i], memo);
19+
int minus = solve(nums, target, i + 1, sum - nums[i], memo);
20+
21+
// Store the result in the memo
22+
memo[key] = plus + minus;
23+
24+
return memo[key];
25+
}
26+
27+
int findTargetSumWays(vector<int>& nums, int target) {
28+
unordered_map<string, int> memo;
29+
return solve(nums, target, 0, 0, memo);
30+
}
31+
};
32+
33+
34+
// ************************** Approach 2 ***************************************
35+
36+
class Solution {
37+
public:
38+
int S;
39+
int solve(vector<int>& nums, int &target, int i, int sum, vector<vector<int>>& t) {
40+
if(i == nums.size()) {
41+
return sum == target ? 1 : 0;
42+
}
43+
44+
if(t[i][sum+S] != INT_MIN) {
45+
return t[i][sum+S];
46+
}
47+
int plus = solve(nums, target, i+1, sum+nums[i], t);
48+
int minus = solve(nums, target, i+1, sum-nums[i], t);
49+
50+
return t[i][sum+S] = plus+minus;
51+
}
52+
53+
int findTargetSumWays(vector<int>& nums, int target) {
54+
int n = nums.size();
55+
S = accumulate(begin(nums), end(nums), 0);
56+
vector<vector<int>> t(n, vector<int>(2*S+1, INT_MIN));
57+
return solve(nums, target, 0, 0, t);
58+
}
59+
};
60+
61+
// *************************** Approach 3 *************************
62+
63+
class Solution {
64+
public:
65+
int S;
66+
int solve(vector<int>& nums, int &target, int i, int sum, vector<vector<int>>& t) {
67+
if(i == nums.size()) {
68+
return sum == target ? 1 : 0;
69+
}
70+
71+
if(t[i][sum+S] != INT_MIN) {
72+
return t[i][sum+S];
73+
}
74+
int plus = solve(nums, target, i+1, sum+nums[i], t);
75+
int minus = solve(nums, target, i+1, sum-nums[i], t);
76+
77+
return t[i][sum+S] = plus+minus;
78+
}
79+
80+
int findTargetSumWays(vector<int>& nums, int target) {
81+
int n = nums.size();
82+
S = accumulate(begin(nums), end(nums), 0);
83+
vector<vector<int>> t(n, vector<int>(2*S+1, INT_MIN));
84+
return solve(nums, target, 0, 0, t);
85+
}
86+
};
87+
88+
// ************************** Approach 3 ***************************
89+
90+
class Solution {
91+
public:
92+
int t[21][1001];
93+
int subsetSum(vector<int>& nums, int n, int s) {
94+
if(t[n][s] != -1)
95+
return t[n][s];
96+
if(s == 0)
97+
return 1;
98+
if(n == 0)
99+
return 0;
100+
if(nums[n-1] == 0)
101+
return t[n][s] = subsetSum(nums, n-1, s);
102+
103+
if(nums[n-1] <= s)
104+
return t[n][s] = subsetSum(nums, n-1, s-nums[n-1]) + subsetSum(nums, n-1, s);
105+
else
106+
return t[n][s] = subsetSum(nums, n-1, s);
107+
}
108+
109+
int findTargetSumWays(vector<int>& nums, int target) {
110+
memset(t, -1, sizeof(t));
111+
int sum = accumulate(begin(nums), end(nums), 0);
112+
auto lambda = [&](const int& x) {
113+
return x == 0;
114+
};
115+
int zeros = count_if(begin(nums), end(nums), lambda);
116+
if(target > sum)
117+
return 0;
118+
119+
if((sum-target) %2 != 0)
120+
return 0;
121+
122+
int s1 = (sum-target)/2;
123+
return pow(2, zeros)*subsetSum(nums, nums.size(), s1);
124+
125+
}
126+
};
127+
128+
//**************************** Approach 4 ******************************
129+
130+
class Solution {
131+
public:
132+
int subsetSum(vector<int>& nums, int s) {
133+
int n = nums.size();
134+
vector<vector<int>> t(n+1, vector<int>(s+1));
135+
136+
for(int col = 0; col < s+1; col++) t[0][col] = 0;
137+
for(int row = 0; row < n+1; row++) t[row][0] = 1;
138+
139+
for(int i = 1; i<n+1; i++) {
140+
for(int j = 1; j<s+1; j++) {
141+
if(nums[i-1] == 0)
142+
t[i][j] = t[i-1][j];
143+
else if(nums[i-1] <= j)
144+
t[i][j] = t[i-1][j-nums[i-1]] + t[i-1][j];
145+
else
146+
t[i][j] = t[i-1][j];
147+
}
148+
}
149+
150+
return t[n][s];
151+
}
152+
153+
int findTargetSumWays(vector<int>& nums, int target) {
154+
int sum = accumulate(begin(nums), end(nums), 0);
155+
auto lambda = [&](const int& x) {
156+
return x == 0;
157+
};
158+
int zeros = count_if(begin(nums), end(nums), lambda);
159+
if(target > sum)
160+
return 0;
161+
162+
if((sum-target) %2 != 0)
163+
return 0;
164+
165+
int s1 = (sum-target)/2;
166+
/*
167+
You could also do like this :
168+
if((sum + target)%2 != 0)
169+
return 0;
170+
171+
int s1 = (sum + target)/2;
172+
But since, target can be negative also as per Leetcode (they have recently changed the constraints),
173+
you need to do :
174+
target = abs(target); before finding s1 and the if condition above
175+
*/
176+
return pow(2, zeros)*subsetSum(nums, s1);
177+
}
178+
};

0 commit comments

Comments
 (0)