LeetCode 692. Top K Frequent Words 前K个高频单词 (Java)
题目:
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统计出每个字符串出现的次数。维护一个容量为k的最小堆,将字符串和出现的频率加入到堆中,然后再从堆中遍历出结果集。
注意本题当字符串频率相同时,小的字符串要出现在前面。
程序:
class Solution {
public List<String> topKFrequent(String[] words, int k) {
HashMap<String, Integer> map = new HashMap<>();
for(String word:words){
int num = map.getOrDefault(word, 0);
map.put(word, num + 1);
}
PriorityQueue<Pair<String, Integer>> queue = new PriorityQueue<>((a, b) ->{
return a.getValue().equals(b.getValue()) ? b.getKey().compareTo(a.getKey()) : a.getValue() - b.getValue();
}
);
for(String word:map.keySet()){
int time = map.get(word);
queue.offer(new Pair<>(word, time));
if(queue.size() > k)
queue.poll();
}
List<String> res = new ArrayList<>();
while(!queue.isEmpty())
res.add(queue.poll().getKey());
Collections.reverse(res);
return res;
}
}
LeetCode 692. Top K Frequent Words 前K个高频单词 (Java)的更多相关文章
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- [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] 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 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- [leetcode]692. Top K Frequent Words频率最高的前K个单词
这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...
- [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# 692. Top K Frequent Words
https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...
- 代码随想录算法训练营day12 | leetcode 239. 滑动窗口最大值 347.前 K 个高频元素
基础知识 ArrayDeque deque = new ArrayDeque(); /* offerFirst(E e) 在数组前面添加元素,并返回是否添加成功 offerLast(E e) 在数组后 ...
- 第k大数(前k大数)
题目:设计一组N个数,确定其中第k个最大值 1,普通方法(先排序,然后遍历,得到第k大的数) 注:如果是数组,直接arr[k],我们可以对这个乱序数组按照从大到小先行排序,然后取出前k大,总 ...
随机推荐
- JavaServlet类
"感谢您阅读本篇博客!如果您觉得本文对您有所帮助或启发,请不吝点赞和分享给更多的朋友.您的支持是我持续创作的动力,也欢迎留言交流,让我们一起探讨技术,共同成长!谢谢!" 介绍Ser ...
- Oracle sql 判断全角字符
(lengthb(MC) - length(MC))<>(lengthb(to_single_byte(MC)) - length(to_single_byte(MC)))
- 第 9章 数据分析案例:Python 岗位行情
第 9章 数据分析案例:Python 岗位行情 9.1 数据爬取 (1)打开某招聘网站首页 https://www.lagou.com,选择"全国站",在搜索栏输入 Python, ...
- 第 8章 Python 爬虫框架 Scrapy(下)
第 8章 Python 爬虫框架 Scrapy(下) 8.1 Scrapy 对接 Selenium 有一种反爬虫策略就是通过 JS 动态加载数据,应对这种策略的两种方法如下: 分析 Ajax 请求 ...
- 重磅发布 阿里云数据中台全新产品DataTrust聚焦企业数据安全保障
简介: DataTrust(隐私增强计算产品)是基于阿里云底层多项基础安全能力,经过阿里云数据中台丰富的客户业务实践,构建的一款为企业数据安全流通的产品. 随着包括零售.制造.金融等多行业数字化转型加 ...
- 知乎的 Flink 数据集成平台建设实践
简介: 本文由知乎技术平台负责人孙晓光分享,主要介绍知乎 Flink 数据集成平台建设实践.内容如下: 1. 业务场景 : 2. 历史设计 : 3. 全面转向 Flink 后的设计 : 4. 未来 F ...
- [GPT] js 外部参数怎么传给 setTimeout 的匿名函数 ?
你可以将外部参数作为 setTimeout() 函数的第三个参数传递,然后在匿名函数中使用这个参数.例如: var myParam = "Hello, world!"; setTi ...
- [FE] G2Plot 更新图表的两种方式
第一种是使用 G2Plot 对象上的 changeData 方法,如果不涉及到全局 title 等这些的更改,可以采用这种方式. 也就是说,只有纯数据方面的变动,使用 changeData 更新图表数 ...
- dotnet SemanticKernel 入门 开篇
本文将开坑告诉大家什么是 SemanticKernel 以及如何使用框架 众所周知 GPT 是一个大语言模型,能够参与的输入和输出是文本内容.而想要让 GPT 完成各项功能,则需要对接现有的编程世界. ...
- 局域网内一部分网络设备无法ping通,icmp_seq=1 目标主机不可达
问题: 来自 192.168.2.99 icmp_seq=1 目标主机不可达. 最近想在局域网内搭建一台服务器,打开SSH服务后发现局域网内的一部分设备无法使用,尝试了各种办法都没能解决,重装系统 ...