-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subset Sum Problem.cpp
89 lines (72 loc) · 2.61 KB
/
Subset Sum Problem.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
/*
Problem Link: https://practice.geeksforgeeks.org/problems/subset-sum-problem-1611555638/1
*/
// -----Approach 1: Memoization ------------------------------------------------------------
class Solution{
private:
bool sol(int idx, int target, vector<vector<int>>& dp, vector<int>& arr){
if(target == 0) return true;
if(idx == 0) return (arr[0] == target);
if(dp[idx][target] != -1) return dp[idx][target];
bool nonPick= sol(idx-1, target, dp, arr);
bool pick= false;
if(target >= arr[idx]){
pick= sol(idx-1, target - arr[idx], dp, arr);
}
return dp[idx][target]= pick or nonPick;
}
public:
bool isSubsetSum(vector<int>arr, int sum){
// code here
int n= arr.size();
vector<vector<int>> dp(n, vector<int>(sum+1, -1));
return sol(n-1, sum, dp, arr);
}
};
// -----Approach 2: Tabulation ------------------------------------------------------------
class Solution{
public:
bool isSubsetSum(vector<int>arr, int sum){
int n= arr.size();
vector<vector<bool>> dp(n, vector<bool>(sum+1, false));
for(int i=0; i<n; i++){
dp[i][0]= true; // 1st base condition
}
if(arr[0] <= sum) dp[0][arr[0]]= sum; // 2nd base condition
for(int idx=1; idx<n; idx++){
for(int target=1; target<=sum; target++){
bool nonPick= dp[idx-1][target];
bool pick= false;
if(target >= arr[idx]){
pick= dp[idx-1][target - arr[idx]];
}
dp[idx][target]= pick | nonPick;
}
}
return dp[n-1][sum];
}
};
// -----Approach 3: Space Optimization ------------------------------------------------------------
class Solution{
public:
bool isSubsetSum(vector<int>arr, int sum){
// space optimization -> O(n) from O(sum*n)
int n= arr.size();
vector<bool> prev(sum+1, false), curr(sum+1, false);
prev[0]= true;
if(arr[0] <= sum) prev[arr[0]]= sum;
for(int idx=1; idx<n; idx++){
curr[0] = true;
for(int target=1; target<=sum; target++){
bool nonPick= prev[target];
bool pick= false;
if(target >= arr[idx]){
pick= prev[target - arr[idx]];
}
curr[target]= pick | nonPick;
}
prev= curr;
}
return prev[sum];
}
};