Skip to content

Commit b78a9a5

Browse files
authored
Create 2597. The Number of Beautiful Subsets (#486)
2 parents 6e5de0b + 8732acb commit b78a9a5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2597. The Number of Beautiful Subsets

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int ans;
4+
unordered_map<int, int> rec;
5+
int beautifulSubsets(vector<int>& nums, int k) {
6+
sort(nums.begin(), nums.end());
7+
ans = 0;
8+
vector<int> tmp;
9+
int n = nums.size();
10+
for (int i = 0; i < n; ++i) {
11+
rec[nums[i]]++;
12+
recursion(i+1, n, tmp, nums, k);
13+
rec[nums[i]]--;
14+
}
15+
return ans;
16+
}
17+
void recursion(int pos, int n, vector<int> &tmp, vector<int>& nums, int k) {
18+
if (pos == n) {
19+
ans++;
20+
return;
21+
}
22+
if (rec[nums[pos] - k] == 0) {
23+
rec[nums[pos]]++;
24+
recursion(pos + 1, n, tmp, nums, k);
25+
rec[nums[pos]]--;
26+
}
27+
recursion(pos + 1, n, tmp, nums, k);
28+
}
29+
};

0 commit comments

Comments
 (0)