- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3396.java
34 lines (31 loc) · 998 Bytes
/
_3396.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
packagecom.fishercoder.solutions.fourththousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_3396 {
publicstaticclassSolution1 {
publicintminimumOperations(int[] nums) {
intops = 0;
Map<Integer, Integer> map = newHashMap<>();
for (intnum : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
inti = 0;
while (!allDistinct(map)) {
ops++;
inttarget = i + 3;
for (; i < target && i < nums.length; i++) {
map.put(nums[i], map.get(nums[i]) - 1);
}
}
returnops;
}
privatebooleanallDistinct(Map<Integer, Integer> map) {
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > 1) {
returnfalse;
}
}
returntrue;
}
}
}