forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1481.java
33 lines (30 loc) · 1.11 KB
/
_1481.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
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class _1481 {
public static class Solution1 {
public int findLeastNumOfUniqueInts(int[] arr, int k) {
Map<Integer, Integer> unSortedMap = new HashMap<>();
for (int num : arr) {
unSortedMap.put(num, unSortedMap.getOrDefault(num, 0) + 1);
}
//LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<Integer, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
int leastUniq = 0;
for (int key : sortedMap.keySet()) {
int count = sortedMap.get(key);
if (k >= count) {
k -= count;
} else {
leastUniq++;
}
}
return leastUniq;
}
}
}