forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #6
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
6 commits
Select commit
Hold shift + click to select a range
9c45c64
Create 698.Partition-to-K-Equal-Sum-Subsets_v2.cpp
wisdompeak 78428ce
Rename 698.Partition-to-K-Equal-Sum-Subsets.cpp to 698.Partition-to-K...
wisdompeak 0a73a82
Update Readme.md
wisdompeak b16426b
Update Readme.md
wisdompeak 248b8a7
Update 698.Partition-to-K-Equal-Sum-Subsets_v2.cpp
wisdompeak 4b4e9e5
Update Readme.md
wisdompeak 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
File renamed without changes.
25 changes: 25 additions & 0 deletions
DFS/698.Partition-to-K-Equal-Sum-Subsets/698.Partition-to-K-Equal-Sum-Subsets_v2.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,25 @@ | ||
class Solution { | ||
public: | ||
bool canPartitionKSubsets(vector<int>& nums, int k) | ||
{ | ||
int n = nums.size(); | ||
int sum = accumulate(nums.begin(), nums.end(), 0); | ||
if (sum%k!=0) return false; | ||
int target = sum / k; | ||
|
||
vector<int>dp(1<<n, -1); | ||
dp[0] = 0; | ||
for (int state=0; state<(1<<n); state++) | ||
{ | ||
if (dp[state]==-1) continue; | ||
for (int i=0; i<n; i++) | ||
{ | ||
if (!((state>>i)&1) && (dp[state]+nums[i] <= target)) | ||
dp[state + (1<<i)] = (dp[state]+nums[i]) % target; | ||
} | ||
} | ||
|
||
return dp[(1<<n)-1] != -1; | ||
|
||
} | ||
}; |
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.