- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1296.java
51 lines (47 loc) · 1.67 KB
/
_1296.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.TreeMap;
publicclass_1296 {
publicstaticclassSolution1 {
publicbooleanisPossibleDivide(int[] nums, intk) {
if (nums.length % k != 0) {
returnfalse;
}
TreeMap<Integer, Integer> treeMap = newTreeMap();
intmin = nums[0];
for (intnum : nums) {
treeMap.put(num, treeMap.getOrDefault(num, 0) + 1);
min = Math.min(min, num);
}
while (!treeMap.isEmpty()) {
if (!isConsecutiveK(treeMap, min, k)) {
returnfalse;
}
min = findNextMin(treeMap);
if (min == Integer.MIN_VALUE) {
break;
}
}
returntrue;
}
privateintfindNextMin(TreeMap<Integer, Integer> treeMap) {
returntreeMap.isEmpty()
? Integer.MIN_VALUE
: treeMap.entrySet().iterator().next().getKey();
}
privatebooleanisConsecutiveK(TreeMap<Integer, Integer> treeMap, intmin, intk) {
intcount = treeMap.get(min);
treeMap.remove(min);
for (inti = 1; i < k; i++) {
intkey = min + i;
if (!treeMap.containsKey(key) || treeMap.get(key) < count) {
returnfalse;
} elseif (treeMap.get(key) > count) {
treeMap.put(key, treeMap.get(key) - count);
} else {
treeMap.remove(key);
}
}
returntrue;
}
}
}