[leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2]
Example 2:
Input: nums = [1], k = 1 Output: [1]
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.
题目
给定数组,求其中出现频率最高的K个元素。
思路
bucket sort
代码
/*
Time: O(n)
Space: O(n)
*/
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
// freq map
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// bucket sort on freq
List<Integer>[] buckets = new List[nums.length + 1];
for (int i : map.keySet()) {
int freq = map.get(i);
if (buckets[freq] == null) {
buckets[freq] = new ArrayList<>();
}
buckets[freq].add(i);
}
// gather result
List<Integer> res = new ArrayList<>();
for (int i = buckets.length - 1; i >= 0; --i) {
if (buckets[i] == null) continue;
for (int item : buckets[i]) {
res.add(item);
if (k == res.size()) return res;
}
}
return res;
}
}
思路
priorityqueue to track Top K Frequent Elements
1. Using HashMap and PriorityQueue
2. Build a HashMap to get the item frequency
3. PriorityQueue (minHeap)
4. If the PQ size > k, then we pop the lowest value in the PQ
跟[leetcode]692. Top K Frequent Words K个最常见单词完全思路一致
代码
/*
Time: O(nlogk)
Space: O(k)
*/
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
// freq map
Map<Integer, Integer> map = new HashMap();
for(int num : nums){
map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
}
// maintain a k-size minHeap
PriorityQueue <Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>((entry1, entry2) -> entry2.getValue() - entry1.getValue());
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
minHeap.add(entry);
}
List<Integer> result = new ArrayList<>();
while(!minHeap.isEmpty() && result.size() < k){
result.add(0, minHeap.remove().getKey());
}
return result;
}
}
[leetcode]347. Top K Frequent Elements K个最常见元素的更多相关文章
- [leetcode]692. Top K Frequent Words K个最常见单词
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- 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 Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
- LeetCode 【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
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
随机推荐
- Django下的templates 和 static静态文件
如果Django顶层目录中没有templates的话,就自己新建一个Directory ,这个文件是存放html文件的 1)如果在views里面用render(request,"" ...
- leetcode ex3 找出穿过最多点的直线 Max Points on a Line
题目 https://oj.leetcode.com/problems/max-points-on-a-line/ 答案与分析 http://www.aiweibang.com/yuedu/18326 ...
- 2312--1.3.4 Prime Cryptarithm 牛式
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- C#项目单步调试莫名结束问题
今天在调试一个问题时,单步跟踪,走到某一步时突然跳出了调试,后面很多断点一个都不进来. 经过更细致的一步步调试(进入每个函数查看),定位到如下一段代码有问题: 原因是:size = 3,buff_id ...
- Spring事务管理——回滚(rollback-for)控制
探讨Spring事务控制中,异常触发事务回滚原理.文章进行了6种情况下的Spring事务是否回滚. 以下代码都是基于Spring与Mybatis整合,使用Spring声明式事务配置事务方法. 1.不捕 ...
- css实现角标
效果图: 简单方式可以使用背景图片,但这里我使用的css来实现,最笨的方式是使用矩形div然后旋转遮挡就可以, <div class='checked-item'> 角标实现 < ...
- Windows上传代码到github操作指导
操作环境 Windows7(32bit) 前提条件 1.完成msysgit工具安装.下载路径:官网或百度网盘路径Git-2.15.0-32-bit.exe.安装方法为一路Next按照默认选项执行就可以 ...
- iptables学习
droidwall.sh #!/system/bin/sh IPTABLES=iptables BUSYBOX=busybox GREP=grep ECHO=echo # Try to find bu ...
- C# WINFORM 打包数据库
实现效果:安装项目时直接附加数据库. 1.首先在需要部署的项目的解决方案资源管理器中新建一个安装项目 2.在安装项目的文件视图中,右键[应用程序文件夹]->[添加]->[项目输出] ...
- web前端基础知识!
[HTML文档的基本结构和语法][基本结构]: <HTML> HTML 文件开始 <HEAD> HTML 文件的头部开始 <title> 网页的标题</tit ...