-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
... ... >_
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
#include <functional> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
/** | ||
* @brief LC698: partition to K equal sum subsets | ||
* Time: O(N * 2^N), Space: O(2^N), DP | ||
* | ||
* @param nums | ||
* @param k | ||
* @return true | ||
* @return false | ||
*/ | ||
bool canPartitionKSubsets(vector<int>& nums, int k) { | ||
int sum = 0; | ||
for (int num : nums) { | ||
sum += num; | ||
} | ||
if (sum % k != 0) { | ||
return false; | ||
} | ||
int per = sum / k; | ||
sort(nums.begin(), nums.end()); | ||
if (nums.back() > per) { | ||
return false; | ||
} | ||
int len = nums.size(); | ||
vector<bool> dp(1 << len, true); | ||
// function<bool(int, int)> dfs = [&](int s, int p) -> bool { | ||
function<bool(int, int)> dfs = [&](int s, int p) -> bool { | ||
if (s == 0) return true; | ||
|
||
if (!dp[s]) return dp[s]; | ||
|
||
dp[s] = false; | ||
for (int i = 0; i < len; i++) { | ||
if (nums[i] + p > per) { | ||
break; | ||
} | ||
if ((s >> i) & 1) { | ||
if (dfs(s ^ (1 << i), (p + nums[i]) % per)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
return dfs((1 << len) - 1, 0); | ||
} | ||
}; |