-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25_TargetSum.cpp
147 lines (130 loc) · 3.58 KB
/
25_TargetSum.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// https://leetcode.com/problems/target-sum/
// You are given an integer array nums and an integer target.
// You want to build an expression out of nums by adding one of the
// symbols '+' and '-' before each integer in nums and then concatenate
// all the integers.
// For example, if nums = [2, 1], you can add a '+' before 2 and a '-'
// before 1 and concatenate them to build the expression "+2-1".
// Return the number of different expressions that you can build, which
// evaluates to target.
#include <bits/stdc++.h>
using namespace std;
class Solution3
{
// Recursion : subset Sum : Memoization
public:
int findTargetSumWays(vector<int> &nums, int target)
{
int n = nums.size();
int sumNums = accumulate(nums.begin(), nums.end(), 0);
target = (sumNums + target) / 2;
vector<vector<int>> dp(n, vector<int>(target + 1, -1));
return solve(nums, dp, target, n - 1);
}
int solve(vector<int> nums, vector<vector<int>> &dp, int target, int i)
{
if (i == 0)
return nums[i] == target;
if (target == 0)
return 1;
if (dp[i][target] != -1)
return dp[i][target];
// Pick : Dont pick if nums[i] > target
int p = 0;
if (nums[i] <= target)
p = solve(nums, dp, target - nums[i], i - 1);
// noPick
int np = solve(nums, dp, target, i - 1);
return dp[i][target] = p + np;
}
};
class Solution2
{
// Recursion : subset SUm
public:
int findTargetSumWays(vector<int> &nums, int target)
{
int n = nums.size();
int sumNums = accumulate(nums.begin(), nums.end(), 0);
target = (sumNums + target) / 2;
return solve(nums, target, n - 1);
}
int solve(vector<int> nums, int target, int i)
{
if (i == 0)
return nums[i] == target;
if (target == 0)
return 1;
// Pick : Dont pick if nums[i] > target
int p = 0;
if (nums[i] <= target)
p = solve(nums, target - nums[i], i - 1);
// noPick
int np = solve(nums, target, i - 1);
return p + np;
}
};
class Solution1
{
// Recursion : Optimised
public:
int findTargetSumWays(vector<int> &nums, int target)
{
int n = nums.size();
return solve(nums, target, n - 1);
}
int solve(vector<int> &nums, int target, int n)
{
// Base Condition
if (n == -1)
{
if (target == 0)
{
return 1;
}
return 0;
}
// Assign positive
int pos = solve(nums, target - nums[n], n - 1);
// Assign negative
int neg = solve(nums, target + nums[n], n - 1);
return pos + neg;
}
};
class Solution
{
// BruteForce : Recursion
public:
int findTargetSumWays(vector<int> &nums, int target)
{
int n = nums.size(), count = 0;
solve(nums, count, target, n - 1);
return count;
}
void solve(vector<int> &nums, int &count, int target, int n)
{
// Base Condition
if (n == -1)
{
if (target == 0)
{
++count;
}
return;
}
// Assign positive
solve(nums, count, target - nums[n], n - 1);
// Assign negative
solve(nums, count, target + nums[n], n - 1);
}
};
int main()
{
vector<int> nums = {1, 1, 1, 1, 1};
int target = 3;
Solution2 Obj1;
cout << Obj1.findTargetSumWays(nums, target);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}