[LC] 347. Top K Frequent Elements
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]
class Solution {
public List<Integer> topKFrequent(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
for (Integer num: nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// keep a top frequency heap
PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>((a, b) ->
a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()): a.getValue() - b.getValue()
);
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
pq.offer(entry);
if (pq.size() > k) {
pq.poll();
}
}
List<Integer> res = new ArrayList<>();
// while (!pq.isEmpty()) {
// res.add(0, pq.poll().getKey());
// }
while (!pq.isEmpty()) {
res.add(pq.poll().getKey());
}
return res;
}
}
public class Solution {
public String[] topKFrequent(String[] combo, int k) {
// Write your solution here.
Map<String, Integer> map = new HashMap<>();
for (String s: combo) {
map.put(s, map.getOrDefault(s, 0) + 1);
}
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
return a.getValue().compareTo(b.getValue());
}
});
for (Map.Entry<String, Integer> mymap: map.entrySet()) {
pq.offer(mymap);
if (pq.size() > k) {
pq.poll();
}
}
String[] res = new String[pq.size()];
for (int i = res.length - 1; i >= 0; i--) {
res[i] = pq.poll().getKey();
}
return res;
}
}
[LC] 347. Top K Frequent Elements的更多相关文章
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 K个最常见元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 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 = [ ...
- 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 ...
- 347. Top K Frequent Elements 最常用的k个元素
[抄题]: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
随机推荐
- hadoop搭建一:虚拟机网络配置和基础(未完成)
基于VMware 15+CentOS 7+Hadoop 2.6,hadoop的搭建主要用于个人学习,水平有限. hadoop搭建一:虚拟机网络配置和基础 hadoop搭建二:hadoop全分布搭建 h ...
- [XNUCA2019Qualifier]EasyPHP
0x00 知识点 预期解中知识点: htaccess生效 如果尝试上传htaccess文件会发现出现响应500的问题,因为文件尾有Just one chance 这里采用# \的方式将换行符转义成普通 ...
- 瑞士军刀DLib的VS2015编译
Dlib的官方解释是: Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creati ...
- Python Learning Day4
---恢复内容开始--- 遇到的模块 NumPy:多维数组的有效操作. 高效的数学函数. Matplotlib:可视化:2D和(最近)3D图 SciPy:大型库实现各种数值算法,例如: 线性和非线性方 ...
- 个人安装GO1.13.6版本指南手册之搭建环境
因好奇而走进go语言,让你不在只闻其声,不见其形. https://golang.org/doc/install:这里是go语言的官网文档.吃不透英文,终究会被限制在有限的区域,一词词的吃透. 安装包 ...
- Mycat简介及适用场景
官网:http://www.mycat.io/ 一.Mycat是什么 Mycat是一个开源的分布式数据库系统,是一个实现了 MySQL 协议的的 Server,前端用户可以把它看作是一个数据库代理,用 ...
- 前端 Docker 镜像体积优化
如果 2019 年技术圈有十大流行词,容器化肯定占有一席之地,随着 Docker 的风靡,前端领域应用到 Docker 的场景也越来越多,本文主要来讲述下开源的分布式图数据库 Nebula Graph ...
- 怎么调出原生态launcher
adb shell am start -n com.android.launcher3/.Launcher
- CI项目设计Redis队列
项目开发过程中需要设计提供可平衡的处理多个用户请求的队列. 需求: 当用户登录后,查看系统中已经登录的管理员队列,然后查看后台管理员的处理能力,如果已经不能处理新的请求,则把该管理员从处理队列中删除, ...
- 使用pyintaller打包python3.6项目,并用c#调用该脚本
一.pythoninstaller 打包python项目 前提:安装python3.6环境+pycharm 1. 安装pyinstaller pip install pyinstaller 2. cm ...