Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b61f479

Browse files
Create 3685.Subsequence-Sum-After-Capping-Elements.cpp
1 parent a7f8569 commit b61f479

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<bool> subsequenceSumAfterCapping(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
vector<int>dp(k+1,0);
6+
dp[0] = 1;
7+
vector<bool>rets(n, 0);
8+
9+
sort(nums.begin(), nums.end());
10+
int i = 0;
11+
for (int x=1; x<=n; x++) {
12+
while (i<n && nums[i]<x) {
13+
for (int c = k; c>=1; c--) {
14+
if (c<nums[i]) break;
15+
dp[c] |= dp[c-nums[i]];
16+
}
17+
i++;
18+
}
19+
int m = n-i;
20+
for (int j=0; j<=m && j*x<=k; j++) {
21+
if (dp[k-j*x]) {
22+
rets[x-1] = 1;
23+
break;
24+
}
25+
}
26+
}
27+
return rets;
28+
}
29+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /