-
Notifications
You must be signed in to change notification settings - Fork 3
/
698.cpp
35 lines (32 loc) · 1.04 KB
/
698.cpp
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
class Solution {
public:
bool canPartitionKSubsets(vector<int>& nums, int k) {
int n = nums.size();
sort(nums.begin(), nums.end());
int sum = 0;
for (auto& num : nums) sum += num;
if (sum % k) return false;
sum /= k;
if (nums[n - 1] > sum) return false;
vector<bool> dp(1 << n, false);
dp[0] = true;
vector<int> total(1 << n, 0);
for (int state = 0; state < (1 << n); ++state) {
if (!dp[state]) continue;
// find next state
for (int i = 0; i < n; ++i) {
if ((state & (1 << i)) == 0) {
if (nums[i] <= (sum - (total[state] % sum))) {
dp[state | (1 << i)] = true;
total[state | (1 << i)] = total[state] + nums[i];
}
// due to sorting
else {
break;
}
}
}
}
return dp[(1 << n) - 1];
}
};