public boolean canPartitionKSubsets(int[] nums, int k) {
int sum = 0;
for(int num : nums) {
sum += num;
}
if(sum % k != 0) {
return false;
}
int target = sum / k;
return helper(target, nums, 0, k, 0);
}
private boolean helper(int target, int[] nums, int preSum, int remain, int start) {
if(remain == 1) {
return true;
}
if(preSum == target) {
return helper(target, nums, 0, remain  1, 0);
}
for(int i = start; i < nums.length; i++) {
if(helper(target, nums, preSum + nums[i], remain, i + 1)) {
return true;
}
}
return false;
}
Java: standard backtracking solution, 21ms


@D3Hunter Yes, u are right.
Actually, in contest, I sort the nums and add a if statement
if(nums[nums.length  1] > target) return false;

