这里是两道题的题目描述:
LeetCode-698.划分为k个相等的子集
LeetCode-416.分割等和子集
LeetCode-698.划分为k个相等的子集
题解代码:
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int sum=0;
int maxValue=0;
for(int i=0;i<nums.length;i++)
{
sum+=nums[i];
maxValue=Math.max(maxValue,nums[i]);
}
if(sum%k!=0 || maxValue>sum/k)
{
return false;
}
int target=sum/k;
return backTrace(nums,target,k,0,0,new boolean[nums.length]);
}
boolean backTrace(int[] nums,int target,int k,int curDiv,int start,boolean[] walked)
{
if(k==0