-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartitionKSubsets.java
67 lines (59 loc) · 2.02 KB
/
PartitionKSubsets.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package org.example;
import java.util.Arrays;
public class PartitionKSubsets {
static class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
int[] visited = new int[nums.length];
int total = 0;
for (int i=0; i<nums.length; i++){
total += nums[i];
}
if (total % k != 0){
return false;
}
else{
int partitionSum = total / k;
Arrays.sort(nums);
int mid = (nums.length) / 2;
for (int l=0;l<mid;l++){
int temp = nums[l];
nums[l] = nums[nums.length-1-l];
nums[nums.length-1-l] = temp;
}
return checkPossible(visited, partitionSum, nums, k, partitionSum, 0);
}
}
private boolean checkPossible(int[] visited, int left, int[] nums, int times, int standard, int nextIndex){
if (times == 1){
return true;
}
// if (left<0){
// return false;
// }
if (left == 0){
return checkPossible(visited, standard, nums, times-1, standard, 0);
}
boolean flag;
for (int j=nextIndex; j<visited.length;j++){
if (visited[j] != 1){
if (left-nums[j]<0 || (j>0 && nums[j]==nums[j-1])){
continue;
}
visited[j] = 1;
flag = checkPossible(visited, left-nums[j], nums, times, standard, j+1);
if (flag){
return true;
}
visited[j] = 0;
}
}
return false;
}
}
public static void main(String[] args) {
int[] nums = new int[]{1,1,1,1,2,2,2,2};
int k = 4;
Solution s = new Solution();
System.out.println(s.canPartitionKSubsets(nums, k));
}
}