LeetCode - Top K Frequent Words
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first. Example 1:
Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:
Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Input words contain only lowercase letters.
Follow up:
Try to solve it in O(n log k) time and O(n) extra space.
先用 hashmap 存储string 和出现次数的映射, 然后insert并维持一个size为K的priorityqueue (注意要自己定义compare 函数),最后会得到top Kth words 但是是从小到大排序,注意要reverse:
class Solution {
public List<String> topKFrequent(String[] words, int k) {
if(words == null || words.length == 0 || k <= 0){
return new ArrayList<String>();
}
Map<String, Integer> map = new HashMap<>();
PriorityQueue<Map.Entry<String,Integer>> pq = new PriorityQueue<>((e1, e2) -> compareElement(e1,e2));
for(String str : words){
map.put(str, map.getOrDefault(str, 0)+1);
}
for(Map.Entry<String,Integer> entry : map.entrySet()){
pq.add(entry);
if(pq.size() > k){
pq.poll();
}
}
List<String> res = new ArrayList<>();
while(!pq.isEmpty()){
res.add(pq.poll().getKey());
}
Collections.reverse(res);
return res;
} private int compareElement(Map.Entry<String,Integer> e1, Map.Entry<String,Integer> e2){
if(e1.getValue() - e2.getValue() != 0)
{return e1.getValue() - e2.getValue();}
else
{return e2.getKey().compareTo(e1.getKey());}
}
}
LeetCode - Top K Frequent Words的更多相关文章
- [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 ...
- [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 ...
- LeetCode "Top K Frequent Elements"
A typical solution is heap based - "top K". Complexity is O(nlgk). typedef pair<int, un ...
- 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]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 ...
- [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 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
随机推荐
- vue2整个项目中,数据请求显示loading图
一般项目中,有时候会要求,你在数据请求的时候显示一张gif图片,然后数据加载完后,消失.这个,一般只需要在封装的axios中写入js事件即可.当然,我们首先需要在app.vue中,加入此图片.如下: ...
- bzoj2330
题解: 差分约束系统 要我们求最小值 显然就是转化为最长路 然后spfa一下即可 代码: #include<bits/stdc++.h> using namespace std; ; lo ...
- bzoj1798
题解: 同洛谷2023 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; ll p,v,su ...
- 【收集资料】OpenGL学习
1.课本配套网站,有Sample Code和书中的图片等 http://math.ucsd.edu/~sbuss/MathCG/ 2.Visual C++的帮助系统可查阅基本OpenGL函数(不包 ...
- Linux7 下重新安装YUM
所有操作均在ROOT用户下,系统版本是Linux7.0 X86_64: 一.删除原有YUM # rpm -aq|grep yum|xargs rpm -e --nodeps 二.下载yum,注意自己的 ...
- Java集合(续)
java学习笔记 --- 集合 1.定义:集合是一种容器,专门用来存储对象 数组和集合的区别? A:长度区别 数组的长度固定 集合长度可变 B:内容不同 数组存储的是同一 ...
- gmtdefaults locate
http://seisman.blog.ustc.edu.cn/index.php/archives/553
- python 高级语言特性
装饰器decorator的使用 在某公司的一次笔试中面试官出了一道题,使用python 的decorator实现一个函数的执行时间的计算. 分析:关于函数执行时间的计算,那么肯定是执行之前得到一个时间 ...
- sql server 2012的AlwaysOn高可用
一.Alway On高性能组件配置说明: 服务器集群节点:2 服务器的操作系统:windows 2008 Sql server版本:sql server 2012 此配置省略sql server的安装 ...
- 在 Andriod/IOS 程序中使用自定义字体
很早就遇到这个问题,QDAC作者也在这里给出了方案.