Least Number of Unique Integers after K Removals LeetCode solution in Java

1
class Solution {
2
public int findLeastNumOfUniqueInts(int[] arr, int k) {
3
Map<Integer, Integer> freqMap = new HashMap<>();
4
for (int a : arr) freqMap.put(a, freqMap.getOrDefault(a, 0) + 1);
5
PriorityQueue<Integer> pq =
6
new PriorityQueue<>((i1, i2) -> Integer.compare(freqMap.get(i1), freqMap.get(i2)));
7
pq.addAll(freqMap.keySet());
8
while (k > 0 && !pq.isEmpty()) {
9
int element = pq.poll();
10
int toBeDeleted = Math.min(k, freqMap.get(element));
11
k -= toBeDeleted;
12
if (toBeDeleted < freqMap.get(element)) pq.add(element);
13
}
14
return pq.size();
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0