- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3016.java
35 lines (33 loc) · 1.41 KB
/
_3016.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
packagecom.fishercoder.solutions.fourththousand;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.PriorityQueue;
publicclass_3016 {
publicstaticclassSolution1 {
publicintminimumPushes(Stringword) {
Map<Character, Integer> map = newHashMap<>();
for (charc : word.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
PriorityQueue<Map.Entry<Character, Integer>> maxHeap =
newPriorityQueue<>((a, b) -> b.getValue() - a.getValue());
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
maxHeap.offer(entry);
}
int[] possibleSets = newint[] {1, 2, 3, 4};
intdigitsLength = 8; // a total of 8 digits that can be assigned
Map<Character, Integer> assigned = newHashMap<>();
for (intj = 0; j < possibleSets.length && !maxHeap.isEmpty(); j++) {
for (inti = 0; i < digitsLength && !maxHeap.isEmpty(); i++) {
Map.Entry<Character, Integer> curr = maxHeap.poll();
assigned.put(curr.getKey(), possibleSets[j]);
}
}
intans = 0;
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
ans += entry.getValue() * assigned.get(entry.getKey());
}
returnans;
}
}
}