首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这里如何使用最小堆来解决这个问题?

这里如何使用最小堆来解决这个问题?
EN

Stack Overflow用户
提问于 2016-05-02 15:19:34
回答 1查看 411关注 0票数 1

我想知道在这里如何使用最小堆来解决以下问题。

我想解决的是使用哈希表并保存数字的计数。但是我不知道如何使用最小堆来解决这个问题。

给定一个非空的整数数组,返回k个最频繁的元素.

例如,给定1,1, 1, 2,2,3和k=2,返回1,2。

注意:您可以假设k总是有效的,1≤k≤数的唯一元素。您的算法的时间复杂度必须优于O(n log ),其中n是数组的大小。

代码语言:javascript
复制
vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> counts;
        priority_queue<int, vector<int>, greater<int>> max_k;
        for(auto i : nums) ++counts[i];
        for(auto & i : counts) {
            max_k.push(i.second);
            // Size of the min heap is maintained at equal to or below k
            while(max_k.size() > k) max_k.pop();
        }
        vector<int> res;
        for(auto & i : counts) {
            if(i.second >= max_k.top()) res.push_back(i.first);
        }
        return res;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-02 16:00:19

代码的工作方式如下:

代码语言:javascript
复制
for(auto i : nums) ++counts[i];  // Use a map to count how many times the
                                 // individual number is present in input

priority_queue<int, vector<int>, greater<int>> max_k;  // Use a priority_queue
                                                       // which have the smallest
                                                       // number at top

for(auto & i : counts) {
    max_k.push(i.second);                 // Put the number of times each number occurred
                                          // into the priority_queue

    while(max_k.size() > k) max_k.pop();  // If the queue contains more than
                                          // k elements remove the smallest
                                          // value. This is done because
                                          // you only need to track the k
                                          // most frequent numbers

vector<int> res;                                         // Find the input numbers
for(auto & i : counts) {                                 // which is among the most
    if(i.second >= max_k.top()) res.push_back(i.first);  // frequent numbers
                                                         // by comparing their
                                                         // count to the lowest of
                                                         // the k most frequent.
                                                         // Return numbers whose 
                                                         // frequencies are among
                                                         // the top k

编辑

正如@SergeyTachenov在这里指出的,结果向量可能返回多个k元素。也许你可以通过这样做来解决这个问题:

代码语言:javascript
复制
for(auto & i : counts) {
    if(i.second >= max_k.top()) res.push_back(i.first);
    if (res.size() == k) break; // Stop when k numbers are found
}

另一个小小的评论

在这里,您并不需要while-statement:

代码语言:javascript
复制
while(max_k.size() > k) max_k.pop();

if-statement就行了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36985936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档