2597. The Number of Beautiful Subsets

problem

solution

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
class Solution {
public:
vector<vector<int>> subsets;
void traverse(int l, vector<int>& path, vector<int> &nums, int k) {

if(!path.empty()) subsets.push_back(path);

for(int i=l; i<nums.size(); ++i) {
if(!path.empty() && !isValid( nums[i], k, path)) continue;
path.push_back(nums[i]);
traverse(i+1, path, nums, k);
path.pop_back();
}

}
bool isValid(int target, int k, vector<int> & path) {

for(int i=path.size()-1 ;i>-1;i--) {

if(target - path[i] ==k){
return false;
}
}
return true;
}
int beautifulSubsets(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
vector<int> path;
traverse(0, path, nums, k);
return subsets.size();

}
};

analysis

  • time complexity O(2^n*n)
  • space complexity O(2^n)