[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 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
题目
思路
1. Using HashMap and PriorityQueue
2. Build a HashMap to get the word frequency
3. PriorityQueue (minHeap) - if the counts are same return higher lexicographically word first so that the lower remains in the queue
4. We add each entry to PQ. If the PQ size > k, then we pop the lowest value in the PQ.
跟[leetcode]347. Top K Frequent Elements K个最常见元素思路完全一致
代码
/*
Time: O(nlogk)
Space: O(n)
*/ class Solution {
public List<String> topKFrequent(String[] words, int k) {
List<String> result = new LinkedList<>();
// freq map
Map<String, Integer> map = new HashMap<>();
for(String s: words){
map.put(s, map.containsKey(s) ? map.get(s) + 1 : 1);
}
// b.getKey().compareTo(a.getKey()) 处理了频率相同,按字典顺序排列的题意要求
PriorityQueue<Map.Entry<String, Integer>> minHeap = new PriorityQueue<>(
(a,b) -> a.getValue()==b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue()-b.getValue()
); for(Map.Entry<String, Integer> entry: map.entrySet())
{
minHeap.offer(entry);
if(minHeap.size()>k)
// 自定义从小到大的顺序保证了poll出来的是较小值,留在minHeap里的是较大值
minHeap.poll();
} while(!minHeap.isEmpty())
result.add(0, minHeap.poll().getKey()); return result;
}
}
[leetcode]692. Top K Frequent Words K个最常见单词的更多相关文章
- [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
https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...
- [leetcode]692. Top K Frequent Words频率最高的前K个单词
这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...
- [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] 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 ...
- [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 ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
随机推荐
- LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的 ...
- RESTFUL 设计风格
RESTFUL 规范总结: Rest是web服务的一种架构风格;使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标 ...
- SSL 链接安全协议的enum
摘自:https://blog.csdn.net/lan_liang/article/details/70948221 在进行HTTPS连接时,需要指定SecurityProtocol.对于.NET ...
- python文件处理指针的移动
控制文件指针移动 with open('a.txt',mode='rt',encoding='utf-8')as f: res=f.read(4) print(res) 强调:只有t模式下read(n ...
- openvpn 批量生成用户脚本
#/bin/bash for user in "$@" do echo "新增用户:$user" if [ -d "/etc/openvpn/clie ...
- 树莓派上的软件安装和卸载命令汇总 [ZT]
转自:http://www.eeboard.com/bbs/thread-40823-1-1.html基础命令 安装软件 apt-get install softname1 softname2 sof ...
- 怎么在idea中新建package包,只有directory选项
http://blog.csdn.net/liyanlei5858/article/details/77320063
- python+爬虫+签名
在公众号,看到一个比较好玩的程序.它使用post的来传送请求,以前没有遇到过.可能是自己,写的程序太少了.查了一下post的用法: 通常,你想要发送一些编码为表单形式的数据——非常像一个 HTML 表 ...
- keras初探
1.对网络的理解: 2.怎样训练,输入已经数据,经过训练,输入测试数据,得到相似数据 3.RNNs 循环神经网络
- ATM--代码
//信1705-2 张小军 20173662 import java.io.*; import java.util.ArrayList; import java.util.Scanner;public ...