-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path698.cpp
More file actions
35 lines (35 loc) · 1011 Bytes
/
698.cpp
File metadata and controls
35 lines (35 loc) · 1011 Bytes
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 x: nums)
sum += x;
int target = sum/k;
if(sum%k>0 || nums[n-1]>target)
return false;
int N = 1<<n;
bool dp[N];
memset(dp,false,sizeof(dp));
dp[0] = true;
int res[N];
memset(res,0,sizeof(res));
for(int state = 0; state<N; state++){
if(!dp[state])
continue;
for(int i=0; i<n; i++){
int tmp = (state | (1<<i));
if(state!=tmp && (dp[tmp]==false)){
if(nums[i]<=target-(res[state]%target)){
dp[tmp] = true;
res[tmp] = res[state] + nums[i];
}
else
break;
}
}
}
return dp[N-1];
}
};