其他分享
首页 > 其他分享> > Top K Frequent Elements

Top K Frequent Elements

作者:互联网

Link: https://leetcode.com/problems/top-k-frequent-elements/

Constraints:

    1 <= nums.length <= 105
    k is in the range [1, the number of unique elements in the array].
    It is guaranteed that the answer is unique.

Idea

We could use priority heap to keep track of the K least frequent elements, by implementing our own comparator. Everytime the size of the heap is larger than K, we pop elemnt from the heap. This way we make sure the remaining K elements are the most frequent.

Code

class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> numberFrequencyMap = new HashMap<>();
        for (int n : nums) {
            if (!numberFrequencyMap.containsKey(n)) {
                numberFrequencyMap.put(n, 1);
            } else {
                numberFrequencyMap.put(n, numberFrequencyMap.get(n) + 1);
            }
        }
        
        PriorityQueue<Element> pq = new PriorityQueue<>();
        for (int n : numberFrequencyMap.keySet()) {
            Element element = new Element(n, numberFrequencyMap.get(n));
            pq.offer(element);
            while (pq.size() > k) {
                pq.poll();
            }
        }
        
        int[] results = new int[k];
        for (int i = 0; i < k && !pq.isEmpty(); i++) {
            Element element = pq.poll();
            results[i] = element.num;
        }
        
        return results;
    }
    
    private class Element implements Comparable<Element> {
        int num;
        int count;
        
        Element(int num, int count) {
            this.num = num;
            this.count = count;
        }
        
        @Override
        public int compareTo(Element other) {
            return this.count - other.count;
        }
    }
}

This question can also be solved using Bucket sort with O(n) time, as shown in this link.

Similar problem:

标签:count,numberFrequencyMap,Elements,num,int,Top,Element,pq,Frequent
来源: https://www.cnblogs.com/blackraven25/p/15119919.html