Least Number of Unique Integers after K Removals (LeetCode 1481): Solution and Explanation
Remove exactly k elements from arr so that as few distinct values as possible are left, and return that number of distinct values.
Intuition
A value only stops counting when every one of its copies has been removed. Removing some but not all copies does nothing for the answer.
So think in groups: each distinct value is a pile, and erasing a pile costs as many removals as it has copies. With a fixed budget k, the way to erase the most piles is to erase the smallest ones.
Sort the pile sizes ascending and pay for them one by one until the next pile costs more than what is left. Any leftover removals go into a surviving pile and do not change the count.
Approach: Count, sort counts, spend k on the smallest
- Count how many times each value appears.
- Sort the counts in increasing order and set
remainingto the number of counts. - For each count
c: ifk < c, stop. Otherwise subtractcfromkand decreaseremaining. - Return
remaining.
Step-by-step example
arr = [5,5,4], k = 1
- Counts: 5 appears 2 times, 4 appears once.
- Sorted counts: [1, 2]. remaining = 2.
- Count 1 (value 4): k = 1 is enough. Remove it: k = 0, remaining = 1.
- Count 2 (value 5): k = 0 is less than 2, so stop.
- Return 1.
Code: Python, Java, C++
1class Solution:
2 def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
3 freq = Counter(arr)
4 counts = sorted(freq.values())
5 remaining = len(counts)
6 for c in counts:
7 if k < c:
8 break
9 k -= c
10 remaining -= 1
11 return remaining1class Solution {
2 public int findLeastNumOfUniqueInts(int[] arr, int k) {
3 Map<Integer, Integer> freq = new HashMap<>();
4 for (int x : arr) freq.merge(x, 1, Integer::sum);
5 List<Integer> counts = new ArrayList<>(freq.values());
6 Collections.sort(counts);
7 int remaining = counts.size();
8 for (int c : counts) {
9 if (k < c) {
10 break;
11 }
12 k -= c;
13 remaining--;
14 }
15 return remaining;
16 }
17}1class Solution {
2public:
3 int findLeastNumOfUniqueInts(vector<int>& arr, int k) {
4 unordered_map<int, int> freq;
5 for (int x : arr) freq[x]++;
6 vector<int> counts;
7 for (auto& [v, c] : freq) counts.push_back(c);
8 sort(counts.begin(), counts.end());
9 int remaining = counts.size();
10 for (int c : counts) {
11 if (k < c) {
12 break;
13 }
14 k -= c;
15 remaining--;
16 }
17 return remaining;
18 }
19};Time and space complexity
O(n log n)
Counting is O(n). Sorting up to n distinct counts is O(n log n) in the worst case.
O(n)
The frequency map and the list of counts can each hold up to n entries.
Other approaches
Bucket by frequency (time O(n), space O(n))
No count exceeds n, so place counts in buckets indexed by frequency and walk the buckets from 1 upward, removing as many whole groups from each bucket as k allows.
Min-heap of counts (time O(n log n), space O(n))
Push every count into a min-heap and pop while the smallest count fits in k. Same greedy order, slightly more overhead than sorting.
Common mistakes
- Removing the most frequent values first. That spends the budget on big piles and erases fewer values.
- Counting a partly removed group as gone. Only groups whose count fits completely in k disappear.
- Continuing the loop after a group does not fit. Every later count is at least as large, so it will not fit either.
- Forgetting k = 0, which must return the number of distinct values unchanged.
Frequently asked questions
Why is removing the smallest groups first optimal?
If an optimal plan erases some group while a smaller group survives, swapping them costs no more removals and erases the same number of groups. Repeating this gives the smallest-first plan.
What happens to leftover removals?
They are taken from any surviving group. That group keeps at least one copy, because its count was larger than the leftover k, so the number of distinct values does not change.
Do we need the actual values or only the counts?
Only the counts. The values are needed just to group equal elements together.