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的更多相关文章

  1. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. 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 ...

  3. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  4. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  5. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  6. 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 ...

  7. 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 ...

  8. 347. Top K Frequent Elements 最常用的k个元素

    [抄题]: Given a non-empty array of integers, return the k most frequent elements. For example,Given [1 ...

  9. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

随机推荐

  1. JSON整理

    1.什么是JSON JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 2.JSON基于两种结构: (1 )“名称/值“对的集合(A co ...

  2. 洛谷 P2196 挖地雷

    题目传送门 解题思路: 记忆化搜索,题目比较坑的地方在于,这是个有向图,给的边是单向边!!!!!!!! AC代码: #include<iostream> #include<cstdi ...

  3. CountUp.js 数字跳转效果小插件

    CountUp.js  实现数字跳转效果的小插件 //调用方法 const easingFn = function (t, b, c, d) { var ts = (t /= d) * t; var ...

  4. URL&HTTP协议详解

    本文来自公开课笔记,主要做知识的记录,谢谢! ·接口测试核心技术--URL&HTTP协议详解. ·URL: 统一资源定位符. 示例: https://ke.qq.com/course/3157 ...

  5. CodeForces 1000B Light It Up(贪心、思维)

    https://codeforces.com/problemset/problem/1000/B 题意: 一个模拟思维题.就是有一盏灯,0时刻开着.n次操作,你可以在其中加入一次操作(或者不加),操作 ...

  6. Android前后台切换的监听

    本文参考这位哥们:https://juejin.im/post/5b87f409e51d4538b0640f58 首先写两个类文件ActivityLifecycleCallbacksAdapter.L ...

  7. delphi try except与try finally语句用法以及区别

    一.异常的来源 在Delphi的应用程序中,下列的情况都比较有可能产生异常. (1)文件处理 (2)内存分配 (3)Windows资源 (4)运行时创建对象和窗体 (5)硬件和操作系统冲突 二.异常的 ...

  8. awk下 gsub函数用法

     (2012-03-27 01:37:28) 标签: awk gsub linux 函数 it 分类: linux gsub函数则使得在所有正则表达式被匹配的时候都发生替换 gsub(regular ...

  9. uni-app真机调试报错request:fail abort解决方法

    Android端真机调试访问本地接口数据时报错:request:fail abort 报错代码 onLoad: function(e) { uni.request({ url: 'http://loc ...

  10. idea指定启动参数、环境变量

    1.  点击Edit Configurations 2 # VM Arguments 是设置的虚拟机的属性 # VM options # 环境变量参数 这里需要指定-D参数 -server -XX:M ...