Poll Max Heap when maxheap.size() > hashmap.size() - k
Min Heap
O(N + (N-k)lg k)
Steps
build a counter map that maps a num to its frequency
build a heap/priority queue that keeps track of k most significant entries
iterate through the final heap and get the keys
Bucket
O(N)
For [1, 1, 1, 2, 2, 2, 3, 3, 3] and k=2, result=[1,2,3]
Solution
Max Heap
// use maxHeap. Put entry into maxHeap so we can always poll a number with largest frequencypublicclassSolution {publicList<Integer> topKFrequent(int[] nums,int k) {Map<Integer,Integer> map =newHashMap<>();for(int n: nums){map.put(n,map.getOrDefault(n,0)+1); }PriorityQueue<Map.Entry<Integer,Integer>> maxHeap =newPriorityQueue<>((a,b)->(b.getValue()-a.getValue()));for(Map.Entry<Integer,Integer> entry:map.entrySet()){maxHeap.add(entry); }List<Integer> res =newArrayList<>();while(res.size()<k){Map.Entry<Integer,Integer> entry =maxHeap.poll();res.add(entry.getKey()); }return res; }}
publicList<Integer>topKFrequent(int[] nums,int k) {Map<Integer,Integer> counterMap =newHashMap<>();for(int num : nums) {int count =counterMap.getOrDefault(num,0);counterMap.put(num, count+1); }PriorityQueue<Map.Entry<Integer,Integer>> pq =newPriorityQueue<>((a, b) ->a.getValue()-b.getValue());for(Map.Entry<Integer,Integer> entry :counterMap.entrySet()) {pq.offer(entry);if(pq.size() > k) pq.poll(); }List<Integer> res =newLinkedList<>();while(!pq.isEmpty()) {res.add(0,pq.poll().getKey()); }return res;}
Bucket
// use an array to save numbers into different bucket whose index is the frequencypublicclassSolution {publicList<Integer> topKFrequent(int[] nums,int k) {Map<Integer,Integer> map =newHashMap<>();for(int n: nums){map.put(n,map.getOrDefault(n,0) +1); }// corner case: if there is only one number in nums, we need the bucket has index 1.List<Integer>[] bucket =newList[nums.length+1];for(int n:map.keySet()){int freq =map.get(n);if(bucket[freq] ==null) bucket[freq] =newLinkedList<>(); bucket[freq].add(n); }List<Integer> res =newLinkedList<>();for(int i =bucket.length-1; i>0&&res.size() < k; --i){if(bucket[i] !=null){res.addAll(bucket[i]); } }return res; }}
TreeMap - Time: O(N + NlogM); Space: O(N)
// use treeMap. Use freqncy as the key so we can get all freqencies in orderpublicclassSolution {publicList<Integer> topKFrequent(int[] nums,int k) {Map<Integer,Integer> map =newHashMap<>();for(int n: nums){map.put(n,map.getOrDefault(n,0)+1); }TreeMap<Integer,List<Integer>> freqMap =newTreeMap<>();for(int num :map.keySet()){int freq =map.get(num);if(!freqMap.containsKey(freq)){freqMap.put(freq,newLinkedList<>()); }freqMap.get(freq).add(num); }List<Integer> res =newArrayList<>();while(res.size()<k){Map.Entry<Integer,List<Integer>> entry =freqMap.pollLastEntry();res.addAll(entry.getValue()); }return res; }}