[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 ...
随机推荐
- LeetCode做题笔记之动态规划
LeetCode之动态规划 时间有限只做了下面这几道:70.338.877.96.120.95.647,后续会继续更新 70:爬楼梯 先来道简单的练练手,一道经典的动态规划题目 可以采用动态规划的备忘 ...
- js保留的关键字
js保留的关键字 break else new var case finally return void catch for switch while continue function this w ...
- Jquery输入框焦点事件及鼠表事件汇总
对于用户的输入框input,我们常常会用ajax来实现与后台的交互.输入框的内容我们可以用.val()方法获取,对于输入框内的事件,我们常用到焦点,如:input.blur.focus.... inp ...
- mysql字符类型总结及常用字符函数
常用字符串函数: concat(s1,s2,s3..) 连接s1,s2,...sn为一个字符串 INSERT(str,x,y,instr)将字符串str从x位置开始,y个字符串替换为字符串 ...
- 在java中如何根据手机号查询号码归属地
1.maven项目中配置 <dependency><groupId>com.googlecode.libphonenumber</groupId><artif ...
- 正则表达式模式修正符 比如/esi
正则表达式模式修正符 比如/esi 作者: 字体:[增加 减小] 类型:转载 下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字 ...
- php对象:get_object_vars(), get_parent_class(),is_subclass_of(),interface_exists()
get_object_vars():获得对象的属性,以关联数组形式返回 get_parent_class():获得对象的父类 is_subclass_of():判断对象是否某类(参数2)的子类实例出的 ...
- Digital Twin——IoT的下一个浪潮
选自Medium 作者:Tomasz Kielar 京东云开发者社区编译 当前的商业格局受到了众多新技术的影响,有时确实让人很难能跟上技术迭新的速度.虽然很多人都熟悉工业4.0和物联网(IoT)这两个 ...
- keras_yolo3程序框架理解
- ubuntu下安装ant
背景介绍 最近终于正式开始填补一下自己在web方面的知识漏洞. 而ant则是必不可少的东西了,要问ant的作用是什么,简单的说,这个软件可以用最简单的方法将你的web应用程序部署到服务器上,是不是很强 ...