forked from SjxSubham/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path416. Partition Equal Subset Sum.cpp
More file actions
43 lines (33 loc) · 1.02 KB
/
416. Partition Equal Subset Sum.cpp
File metadata and controls
43 lines (33 loc) · 1.02 KB
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
public:
int t[201][20001];
// before memo - O(2^n) : memo - O(n * x)
//S.C : O(n*X)
bool canPartition(vector<int>& nums) {
int n = nums.size();
int S = accumulate(nums.begin(), nums.end(),0);
if(S%2 != 0){
return false;
}
memset(t, -1, sizeof(t));
int x = S/2;
return solve(nums, 0, x);
}
bool solve(vector<int>& nums, int i, int x){ // All good also try to solve the GFG problem named as Subset Sum - https://www.geeksforgeeks.org/problems/subset-sums2234/1 solve here
if(x==0){
return true;
}
if(i >= nums.size()){
return false;
}
if(t[i][x] != -1){
return t[i][x];
}
// vector<vector<int>> t(n+1, vector<int>(x+1))
bool take = false;
if(nums[i] <= x){
take = solve(nums, i+1, x- nums[i]);
}
bool not_take = solve(nums, i+1, x);
return t[i][x] = take|| not_take;
}
};