LeetCode "Top K Frequent Elements"
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"的更多相关文章
- [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 ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [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 ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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 ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
随机推荐
- attr和prop
<div class="content-item active"> <table class="table"> <thead> ...
- jquery中的cookie操作
使用前在页面中引入下面的代码 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Cop ...
- Android Spinner控件数据绑定
Java代码
- PHP 数组
// 这里用数字来作为索引 $myArray = array(2012, 'blue', 5, 'BMW'); // 这个用关键字作为索引 $myAssocArray = array('year' = ...
- WinRT知识积累2之MessageDialog应用代码
private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e) { // TODO: 在此处保存页面的唯一状态 ...
- Google加强版权保护
在版权保护方面,我们一直是反面教材,而在场面上Google早已退出我们的世界,所以Google的加强版权保护对国内的互联网不会有太多的影响,即便无法在Google搜索到我们需要的XXX软件破解版,百度 ...
- 58.com qiyi
using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...
- ACM 矩阵题目整理
先从最基础的矩阵快速幂加速递推开始. HDU 1005 Number Sequence |f[n-2],f[n-1]|* |0 B| =|f[n-1], B*f[n-2]+A*f[n-1]|=|f[n ...
- js 上传文件预览
1. FILE API html5提供了FIle和FileReader两个方法,可以读取文件信息并读取文件. 2. example <html> <body> <div ...
- HDU5128 细心、细心、细心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给你n(n < 30)个点的坐标,然后让你求出这n个点能构成的两个最大矩形的面积,有 ...