A typical solution is heap based - "top K". Complexity is O(nlgk).

typedef pair<int, unsigned> Rec;
struct Comp
{
bool operator()(const Rec &r1, const Rec &r2)
{
return r1.second > r2.second;
}
};
class Solution { public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, unsigned> hm;
for(auto&v : nums) hm[v]++; priority_queue<Rec, vector<Rec>, Comp> q;
for(auto &kv : hm)
{
Rec r(kv.first, kv.second);
q.push(r);
if(q.size() > k) q.pop();
} vector<int> ret;
while(!q.empty())
{
ret.push_back(q.top().first);
q.pop();
}
return ret;
}
};

There is a O(n) one indeed - bucketing the frequencies.
https://leetcode.com/discuss/100636/c-o-nlogk-and-o-n-solutions

LeetCode "Top K Frequent Elements"的更多相关文章

  1. [LeetCode] Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  2. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

  3. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  4. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  5. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  6. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  7. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  8. 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 ...

  9. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

随机推荐

  1. PHP二次开发discuz3.2最新体验

    康盛官方于6月4号发布了discuz3.2的正式版,因为这两天一直忙于一个项目,一直没来的及体验,现在抽时间总算是装上了,也体验一把. 根据官方说明:Discuz! X3.2 在继承和完善 Discu ...

  2. centos7.0 64位系统安装 nginx

    1 下载nginx 从nginx官网 http://nginx.org/ 下载新的稳定版本nginx 并上传到linux服务器 2 安装nginx 所需要的扩展 yum -y install pcre ...

  3. Reflector反编译WinForm程序重建项目资源和本地资源

    工具:vs2012..NET Reflector8.1.0.35 要解决的问题: 通过Reflector反编译生成的代码可以编译通过并显示窗体的本地资源和项目资源图片 一.测试项目 两个图片分别放在项 ...

  4. Five More Hacker Tools Every CISO Should Understand

    As we mentioned in the first article, Top Five Hacker Tools Every CISO Should Understand, the role o ...

  5. yii2-user

    https://github.com/dektrium/yii2-user 安装 : composer require "dektrium/yii2-user:0.9.*@dev" ...

  6. mybatis进阶

    1.mybatis一对一映射 Student--Card <?xml version="1.0" encoding="utf-8" ?> <! ...

  7. Rhel6-piranha配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  8. 《大象-Think In UML》读书笔记3

    建模,是指通过对客观事物建立一种抽象的方法用以表征事物并过得对事物本身的理解,同时把这种理解概念化,将这些逻辑概念组织起来,构成一种对所观察的对象的内部结构和工作原理的便于理解的表达. 建模包含两个问 ...

  9. JVM-并发-线程

    线程 1.线程的实现 (1)实现线程主要有3中方式:使用内核线程实现,使用用户线程实现和使用用户线程加轻量级进程混合实现. (2)使用内核线程实现 内核线程就是直接由操作系统内核支持的线程,这种线程由 ...

  10. 深入学习JavaScript: apply 方法 详解(转)——非常好

    主要我是要解决一下几个问题: 1.        apply和call的区别在哪里 2.        什么情况下用apply,什么情况下用call 3.        apply的其他巧妙用法(一般 ...