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大,总 ...
随机推荐
- 《Effective C#》系列之(一)——异常处理与资源管理
请注意,<Effective C#>中的异常处理与资源管理部分实际上是第四章的内容.以下是关于该章节的详细解释. 第四章:异常处理与资源管理 一. 了解异常处理机制 异常处理机制使程序员能 ...
- Oracle with的重复使用(递归)
Oracle with的重复使用(递归) 写力扣的时候学到了新的方法 Recursive WITH Clauses 通常来说如果直接使用with XXX as ()这种,是没发直接使用自身的数据的 例 ...
- 技术解读:英特尔 x86 平台上,AI 能力是如何进行演进的?(附PPT)
简介:AI 生态系统是怎样的?其中又有哪些关键技术? AI 计算力的指数增长意味着,为了解决越来越复杂的用例,即使是 1000 倍的计算性能增长也很容易被消耗.因此,需要通过软件生态系统的助力,才能 ...
- 基于MaxCompute SQL 的半结构化数据处理实践
简介: MaxCompute作为企业级数据仓库服务,集中存储和管理企业数据资产.面向数据应用处理和分析数据,将数据转换为业务洞察.通过与阿里云内.外部服务灵活组合,可构建丰富的数据应用.全托管的数据 ...
- [GPT] 网页中某些dom内容是通过 js 数据异步渲染的,nodejs 怎么获取网页解析这些数据
要处理使用JavaScript异步渲染内容的网页,您可以在 JavaScript 蜘蛛中使用 Puppeter 或 Playwright 等无头浏览器来获取网页,然后与动态渲染的内容进行交互. 下 ...
- [Gse] 高效的Golang中文分析库推荐
优点:用法简单,支持各种语言,基本满足需求. 缺点:默认分词字典文件有 8M 需测试使用速度. 我们可以直接封装一个简单的辅助方法来实现分词功能: // @author cnblogs.com/far ...
- dotnet DirectX 通过 Vortice 控制台使用 ID2D1DeviceContext 绘制画面
在上一篇博客里面告诉大家,如何使用 Vortice 从零开始控制台创建 Direct2D1 窗口.上一篇博客采用的是 CreateDxgiSurfaceRenderTarget 的方式拿到了 ID2D ...
- Azure 无服务器 Function 函数计算服务 dotnet core 3.1 创建和部署入门
本文用的是 世纪互联 的 Azure.cn 版本,这个版本因为是在国内,所以网速会快超级超级多.使用 世纪互联 的版本需要一块钱哦,用一块钱就能进入一个月的免费试用.本文主要告诉小伙伴如何使用 Azu ...
- VisualStudio 如何快速添加一个 Git Tag 推送
在 VisualStudio 的团队管理功能,提供了方便的添加 Tag 的方法,可以新建一个 Tag 添加 Tag 信息,同时推送某个特定的 Tag 到服务器.配合推 Tag 打包 NuGet 的方法 ...
- Solution Set - 点分治
A[POJ1741].给定一棵树,边有权,求长度不超过\(k\)的路径数目. B[HDU4871].给定一张图,边有权,求它的最短路径树上恰含\(k\)个点的路径中最长路径的长度及数目. C[HDU4 ...