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. linux 后台运行命令 nohup命令

    转载:http://if.ustc.edu.cn/~ygwu/blog/archives/000538.html 2005年04月18日 简单而有用的nohup命令在UNIX/LINUX中,普通进程用 ...

  2. canvas滤镜6种效果吗

    昨天写了一个canvas滤镜的取反色效果,今天加一点效果,主要思路都是一样的,改变getImageData.data[]的值,并返回 代码如下: <!DOCTYPE html> <h ...

  3. JS重要知识点

    这里列出了一些JS重要知识点(不全面,但自己感觉很重要).彻底理解并掌握这些知识点,对于每个想要深入学习JS的朋友应该都是必须的. 讲解还是以示例代码搭配注释的形式,这里做个小目录: JS代码预解析原 ...

  4. Android开发--RadioButton的应用

    1.简介 RadioButton为单选按钮,当一个按钮被选中后,点击其他按钮无法使上一个按钮变为未选中状态.RadioGroup是可以容纳多个RadioButton的容器, 通过使用RadioGrou ...

  5. 关于BS响应式的网站建设

    一.首先是导航 html部分: <!-- 导航 --> <nav class="navbar navbar-default navbar-fixed-top"&g ...

  6. IT公司100题-25-求字符串中的最长数字串

    问题描述: 实现一个函数,求出字符串中的连续最长数字串.例如输入”12345cbf3456″,输出”12345″. 函数原型为: void conti_num_max( const char * sr ...

  7. win7 下设置时间格式为yyyy-MM-dd 格式无效的解决方法

    部分win7 64位机器,在时间区域部分设置了时间格式为:yyyy-MM-dd后程序和数据库里面还是原来默认的yyyy/MM/dd格式 打开注册表,搜索 yyyy/MM/dd ,修改为yyyy-MM- ...

  8. 2016年发布APASVO-p波震相自动拾取分析

    Why automatic attractive? large amount of seismic data ; if manually,it depends om experience of ana ...

  9. BZOJ 2763 分层图最短路

    突然发现我不会分层图最短路,写一发. 就是同层中用双向边相连,用单向边连下一层 #include <cstdio> #include <algorithm> #include ...

  10. Oracle 11gR2 安装教学

    官方网址:http://www.oracle.com/index.html 选择你的"操作系统"下载 例如: 环境:x64 Win2012 R2 Oracle:win64_11gR ...