forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
...85.Subsequence-Sum-After-Capping-Elements/3685.Subsequence-Sum-After-Capping-Elements.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution { | ||
public: | ||
vector<bool> subsequenceSumAfterCapping(vector<int>& nums, int k) { | ||
int n = nums.size(); | ||
vector<int>dp(k+1,0); | ||
dp[0] = 1; | ||
vector<bool>rets(n, 0); | ||
|
||
sort(nums.begin(), nums.end()); | ||
int i = 0; | ||
for (int x=1; x<=n; x++) { | ||
while (i<n && nums[i]<x) { | ||
for (int c = k; c>=1; c--) { | ||
if (c<nums[i]) break; | ||
dp[c] |= dp[c-nums[i]]; | ||
} | ||
i++; | ||
} | ||
int m = n-i; | ||
for (int j=0; j<=m && j*x<=k; j++) { | ||
if (dp[k-j*x]) { | ||
rets[x-1] = 1; | ||
break; | ||
} | ||
} | ||
} | ||
return rets; | ||
} | ||
}; |
24 changes: 24 additions & 0 deletions
Dynamic_Programming/3685.Subsequence-Sum-After-Capping-Elements/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
### 3685.Subsequence-Sum-After-Capping-Elements | ||
|
||
对于n个元素,问是否可以组合出和为k的方案,就是一个典型的有界背包问题,用o(n*k)的时间复杂度可解。大致算法是: | ||
```cpp | ||
for (int i=0; i<nums.size(); i++) { | ||
for (int c=k; c>=1; c--) { | ||
dp[c] |= dp[c-nums[i]; | ||
} | ||
} | ||
``` | ||
|
||
对于此题而言,对于每一个给定的x,我们要用nums里所有小于x的元素,以及剩余的元素当做x使用,问是否能组合出和为k的方案。我们暂时先不考虑第二种用法,即只用nums里小于x的元素。我们发现,随着x的增大,其实第一个for循环里可选的元素种类的上界也在单调增大,故总体这依然是一个o(n*k)可解的问题。 | ||
|
||
然后更深入地思考,我们将x从小到大遍历的时候,可以不断提升i,从而加入所有不超过x的新nums[i],就可以不断更新dp。同时我们还需要考虑等于x的元素:因为capping的缘故,这样的元素有n-i个。此时我们需要考虑这额外的n-i个元素能否对于组成k有帮助。显然我们可以用同样的背包思想: | ||
```cpp | ||
for (int j=1; j<n-i; j++) { | ||
if (dp[k-x*j]) { | ||
return true; | ||
break; | ||
} | ||
} | ||
``` | ||
注意,这个过程中我们只是判断是否能组合成k,不会去更新dp。我们始终保持dp[c]表示"能否用所有小于x的元素组成c"。这样,即使x变大,dp的定义依然适用。 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.