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
|
#include <iostream>
#include <vector>
#include <algorithm>
class Solution {
public:
std::vector<bool> subsequenceSumAfterCapping(std::vector<int>& nums, int k) {
int n = nums.size();
std::sort(nums.begin(), nums.end());
std::vector<bool> res(n, false);
std::vector<int> dp(k + 1, 0);
dp[0] = 1;
int ptr = 0;
for (int x = 1; x <= n; ++x) {
while (ptr < n && nums[ptr] <= x) {
for (int j = k; j >= nums[ptr]; --j) {
dp[j] |= dp[j - nums[ptr]];
}
++ptr;
}
int m = n - ptr;
for (int j = 0; j <= m; ++j) {
if (j * x > k) {
break;
}
if (dp[k - j * x]) {
res[x - 1] = true;
break;
}
}
}
return res;
}
};
|