-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_SubsetSum.cpp
68 lines (62 loc) · 1.86 KB
/
04_SubsetSum.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
// Question Link :- https://www.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1
// Subset Sum Problem
// Tabulation
// T.C = O(sum*n)
// S.C = O(sum*n)
class Solution {
public:
bool isSubsetSum(vector<int>arr, int sum) {
int n = arr.size();
bool t[n + 1][sum + 1];
// initialization
for (int i=0; i<n+1; i++) { // i -> size of the array
for (int j=0; j<sum+1; j++) { // j -> target sum (subset sum)
if (i == 0) {
t[i][j] = false;
}
if (j == 0) {
t[i][j] = true;
}
}
}
for (int i = 1; i<n+1; i++) {
for (int j = 1; j<sum+1; j++) {
if (arr[i - 1] <= j) { // if element of arr is smaller than the target sum
t[i][j] = t[i - 1][j - arr[i - 1]] || t[i - 1][j]; // either take or(||) do not take
}
else { // if element of arr is greater than the target sum
t[i][j] = t[i - 1][j];
}
}
}
return t[n][sum]; // at last return T/F
}
};
// Memoization
// T.C = O(sum*n)
// S.C = O(sum*n)
class Solution {
public:
bool solve(vector<int>& arr, int target, int n, vector<vector<bool>>& dp) {
if(target == 0) {
return true;
}
if(n == 0) {
return false;
}
if(dp[n][target] != false) {
return dp[n][target];
}
if(arr[n-1] <= target) {
dp[n][target] = solve(arr, target-arr[n-1], n-1, dp) || solve(arr, target, n-1, dp);
} else {
dp[n][target] = solve(arr, target, n-1, dp);
}
return dp[n][target];
}
bool isSubsetSum(vector<int>& arr, int target) {
int n = arr.size();
vector<vector<bool>> dp(n + 1, vector<bool>(target + 1, false));
return solve(arr, target, n, dp);
}
};