forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_698.cpp
37 lines (35 loc) · 1007 Bytes
/
_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
36
37
//Partition to K Equal Sum Subsets
class Solution {
public:
bool dfs(vector<int> &nums, vector<int> visited, int idx,int k,int currSum,int targetSum){
if(k==1){
//All subsets are found
return true;
}
if(currSum==targetSum){
//one subset found
return dfs(nums,visited,0,k-1,0,targetSum);
}
for(int i=idx;i<nums.size();i++){
if(!visited[i]){
visited[i]=true;
if(dfs(nums,visited,i+1,k,currSum+nums[i],targetSum)){
return true;
}
visited[i]=false;
}
}
return false;
}
bool canPartitionKSubsets(vector<int>& nums, int k) {
vector<int> visited(nums.size(),false);
int sum=0;
for(int i=0;i<nums.size();i++){
sum += nums[i];
}
if(sum%k!=0){
return false;
}
return dfs(nums,visited,0,k,0,sum/k);
}
};