-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (31 loc) · 853 Bytes
/
Copy pathSolution.java
File metadata and controls
41 lines (31 loc) · 853 Bytes
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
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
class Solution
{
public int findLeastNumOfUniqueInts(int[] arr, int k)
{
Map<Integer, Integer> counter = new HashMap<>();
for (int num : arr)
{
counter.put(num, counter.getOrDefault(num, 0) + 1);
}
int min = counter.size();
var sorted = counter.keySet()
.stream()
.sorted(Comparator.comparingInt(counter::get))
.collect(Collectors.toList());
for (int i = 0; i < sorted.size(); i++)
{
int num = sorted.get(i);
int sz = counter.get(num);
if (sz <= k)
{
k -= sz;
min--;
}
}
return min;
}
}