347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements.

For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].

Note:

  • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  • Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

Solution 1: Hash Table, Heap 23ms

Time Complexity: $$O(n+nlogk)$$

这道题给了我们一个数组,让我们统计前k个高频的数字,那么对于这类的统计数字的问题,首先应该考虑用哈希表来做,建立数字和其出现次数的映射,然后再按照出现次数进行排序。我们可以用堆排序来做,使用一个最小堆来按照映射次数从小到大排列,只存当前遍历的数字中topk的数字。当size超过k时,把frequency最小的pop。

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> m;
        for (int& i:nums) ++m[i];
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; // minheap
        for (auto& a:m) {
            pq.push({a.second, a.first});
            if (pq.size() > k) pq.pop();
        }
        vector<int> res;
        while (!pq.empty()) {
            res.push_back(pq.top().second);
            pq.pop();
        }
        return res;
    }
};

Solution 2: Bucket Sort 19ms

Time Complexity: $$O(n)$$

我们还可以使用桶排序,在建立好数字和其出现次数的映射后,我们按照其出现次数将数字放到对应的位置中去,这样我们从桶的后面向前面遍历,最先得到的就是出现次数最多的数字,我们找到k个后返回即可,参见代码如下:

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> m;
        for (auto& a: nums) ++m[a];

        vector<vector<int>> bucket(nums.size()+1);
        for (auto& a: m) bucket[a.second].push_back(a.first);

        vector<int> res;
        for (int i = bucket.size()-1; i >= 0; --i) {
            for (int j = 0; j < bucket[i].size(); ++j) {
                res.push_back(bucket[i][j]);
                if (res.size() == k) return res;
            }
        }
        return res;
    }
};

results matching ""

    No results matching ""