|
47 | 47 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
48 | 48 |
|
49 | 49 | ```python
|
50 |
| - |
| 50 | +class Solution: |
| 51 | + def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int: |
| 52 | + counter = Counter(arr) |
| 53 | + t = sorted(counter.items(), key=lambda x: x[1]) |
| 54 | + for v, cnt in t: |
| 55 | + if k >= cnt: |
| 56 | + k -= cnt |
| 57 | + counter.pop(v) |
| 58 | + else: |
| 59 | + break |
| 60 | + return len(counter) |
51 | 61 | ```
|
52 | 62 |
|
53 | 63 | ### **Java**
|
54 | 64 |
|
55 | 65 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
56 | 66 |
|
57 | 67 | ```java
|
| 68 | +class Solution { |
| 69 | + public int findLeastNumOfUniqueInts(int[] arr, int k) { |
| 70 | + Map<Integer, Integer> counter = new HashMap<>(); |
| 71 | + for (int v : arr) { |
| 72 | + counter.put(v, counter.getOrDefault(v, 0) + 1); |
| 73 | + } |
| 74 | + List<Map.Entry<Integer, Integer>> t = new ArrayList<>(counter.entrySet()); |
| 75 | + Collections.sort(t, Comparator.comparingInt(Map.Entry::getValue)); |
| 76 | + for (Map.Entry<Integer, Integer> e : t) { |
| 77 | + int v = e.getKey(); |
| 78 | + int cnt = e.getValue(); |
| 79 | + if (k >= cnt) { |
| 80 | + k -= cnt; |
| 81 | + counter.remove(v); |
| 82 | + } else { |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + return counter.size(); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### **C++** |
| 92 | + |
| 93 | +```cpp |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + int findLeastNumOfUniqueInts(vector<int>& arr, int k) { |
| 97 | + unordered_map<int, int> counter; |
| 98 | + for (int v : arr) ++counter[v]; |
| 99 | + vector<pair<int, int>> t(counter.begin(), counter.end()); |
| 100 | + sort(t.begin(), t.end(), [](const auto& a, const auto& b) {return a.second < b.second;}); |
| 101 | + for (auto [v, cnt] : t) |
| 102 | + { |
| 103 | + if (k >= cnt) |
| 104 | + { |
| 105 | + k -= cnt; |
| 106 | + counter.erase(v); |
| 107 | + } |
| 108 | + else break; |
| 109 | + } |
| 110 | + return counter.size(); |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
58 | 114 |
|
| 115 | +### **Go** |
| 116 | +
|
| 117 | +```go |
| 118 | +func findLeastNumOfUniqueInts(arr []int, k int) int { |
| 119 | + counter := make(map[int]int) |
| 120 | + for _, v := range arr { |
| 121 | + counter[v]++ |
| 122 | + } |
| 123 | + var t [][]int |
| 124 | + for v, cnt := range counter { |
| 125 | + t = append(t, []int{v, cnt}) |
| 126 | + } |
| 127 | + sort.Slice(t, func(i, j int) bool { |
| 128 | + return t[i][1] < t[j][1] |
| 129 | + }) |
| 130 | + for _, e := range t { |
| 131 | + v, cnt := e[0], e[1] |
| 132 | + if k >= cnt { |
| 133 | + k -= cnt |
| 134 | + delete(counter, v) |
| 135 | + } else { |
| 136 | + break |
| 137 | + } |
| 138 | + } |
| 139 | + return len(counter) |
| 140 | +} |
59 | 141 | ```
|
60 | 142 |
|
61 | 143 | ### **...**
|
|
0 commit comments