Given an array of integers nums and a positive integer k, find whether it’s possible to divide this array into k non-empty subsets whose sums are all equal.
Example 1:
Input: nums = [4, 3, 2, 3, 5, 2, 1], k = 4
Output: True
Explanation: It’s possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Java Solution
The easiest solution to this problem is DFS. We try to place each element to one of the bucket. The following is a Java solution and there is a diagram to show the execution of the helper() method using the given example. Note the improvement in the for loop.
public boolean canPartitionKSubsets(int[] nums, int k) { int sum = 0; for(int num: nums){ sum+=num; } if(sum%k!=0){ return false; } int share = sum/k; //sort array Arrays.sort(nums); int j=nums.length-1; if(nums[j]>share){ return false; } while(j>=0 && nums[j]==share){ j--; k--; } int[] buckets = new int[k]; return helper(j, nums, share, buckets); } //put jth number to each bucket and recursively search public boolean helper(int j, int[] nums, int share, int[] buckets){ if(j<0){ return true; } for(int i=0; i<buckets.length; i++){ if(buckets[i]+nums[j]<=share){ buckets[i]+=nums[j]; if(helper(j-1, nums, share, buckets)){ return true; } buckets[i]-=nums[j]; } if(buckets[i]==0) break;// } return false; } |
//sort array
Arrays.sort(nums);
int j=nums.length-1;
if(nums[j]>share){
return false;
}
while(j>=0 && nums[j]==share){
j–;
k–;
=======
so you sorted out array to find out are there any elements equal share (sum/#buckets)?
why didn’t you just use quik select to get all elements equal share? it is linear.
no offense buddy, what does DFS have to do w/?
it is recursive, back tracking non-optimised algo.