LeetCode source
Build a frequency hash map, then sort using counting sort. Keep adding the integer with the max frequency until you remove at least half of the integers.
-
Count the frequency of each integer in the array using a hash map.
-
Sort the frequencies by counting sort.
-
Keep remove the maximum frequency from the sorted frequencies until removed >= half.
In worst case, the maximum value in frequencies array is 1, this happens when all numbers in array are the uniq.
-
Time Complexity: preprocessing O(n), where build frequency map need O(n) and counting sort need O(n).
-
Space Complexity: O(n). We only used additional n extra space for the hash map and counting vector.
| Status | Runtime | Memory | Language |
|---|---|---|---|
| Accepted | 278 ms | 77.7 MB | cpp |
| Accepted | 19 ms | 5 MB | rust |