347. Top K Frequent Elements
Description
Intuition
Intuitively, we can find a O(N * log(k))
soluton by using a priority queue.
However, we can use bucket sort to achieve O(N)
.
Steps:
- Count the frequency of elements.
- Put into
List<List<Integer>> freqToElements
, the index is thefrequency
- Loop from the tail, add to the result list.