题目:

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:

  1. You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  2. Input words contain only lowercase letters.

Follow up:

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

  1. Top K Frequent Elements 前K个高频元素

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

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

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

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

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

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

    给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意:    你可以假设给定的 k 总是合理的,1 ≤ k ...

  6. [leetcode]692. Top K Frequent Words频率最高的前K个单词

    这个题的排序是用的PriorityQueue实现自动排列,优先队列用的是堆排序,堆排序请看:http://www.cnblogs.com/stAr-1/p/7569706.html 自定义了优先队列的 ...

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

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

  9. 代码随想录算法训练营day12 | leetcode 239. 滑动窗口最大值 347.前 K 个高频元素

    基础知识 ArrayDeque deque = new ArrayDeque(); /* offerFirst(E e) 在数组前面添加元素,并返回是否添加成功 offerLast(E e) 在数组后 ...

  10. 第k大数(前k大数)

    题目:设计一组N个数,确定其中第k个最大值 1,普通方法(先排序,然后遍历,得到第k大的数)      注:如果是数组,直接arr[k],我们可以对这个乱序数组按照从大到小先行排序,然后取出前k大,总 ...

随机推荐

  1. 开源小白到核心开发——我与 sealer 的成长故事

    简介: 下面将以作者本人与 sealer 的一段成长故事来带领大家体验从小白到核心功能开发者的开源之旅,同时本文也作为一篇引领开源小白入门的文章供大家参考. 作者:周欣元 个人简介   大家好,我是周 ...

  2. 阿里云开源业内首个应用多活项目 AppActive,与社区共建云原生容灾标准

    ​简介:继高可用架构团队的 Sentinel.Chaosblade 开源后,第三个重磅高可用产品:应用多活 AppActive 正式开源,形成高可用的三架马车,帮助企业构建稳定可靠的企业级生产系统,提 ...

  3. 重磅发布:微服务引擎 MSE 专业版

    简介: 性能提升 10 倍,更高的 SLA 保障,新用户限时抢购 8 折资源包. 微服务引擎 MSE 专业版发布,支持 Nacos 2.0 ,相比基础版,专业版具有更高的 SLA 保障,性能提升十倍, ...

  4. 基于英特尔® 优化分析包(OAP)的 Spark 性能优化方案

    ​简介: Spark SQL 作为 Spark 用来处理结构化数据的一个基本模块,已经成为多数企业构建大数据应用的重要选择.但是,在大规模连接(Join).聚合(Aggregate)等工作负载下,Sp ...

  5. [FE] Quasar BEX 预览版指南

    BEX(Browser Extension)是 Quasar 基于同一套代码允许编译成浏览器扩展来运行,支持 Firefox & Chrome. 截止目前(2019/12/25), bex 模 ...

  6. 2019-11-29-C#-如何写-DEBUG-输出

    title author date CreateTime categories C# 如何写 DEBUG 输出 lindexi 2019-11-29 08:28:35 +0800 2018-2-13 ...

  7. vue使用bus.js在兄弟组件传值

    A组件往B组件传递数据data 1.src下创建文件eventBus.js,内容: import Vue from 'vue' export default new Vue() 2.在A,B组件分别引 ...

  8. LLM优化:开源星火13B显卡及内存占用优化

    1. 背景 本qiang~这两天接了一个任务,部署几个开源的模型,并且将本地经过全量微调的模型与开源模型做一个效果对比. 部署的开源模型包括:星火13B,Baichuan2-13B, ChatGLM6 ...

  9. 如何对一个新的 VSCode 配置 LaTeX

    texlive 的安装件参考资料 [1]. 往 VSCode 里面装 LaTeX Workshop 插件,也可以直接搜 James-Yu.latex-workshop. Ctrl+Shift+P 打开 ...

  10. C/C++如何写调试宏

    1. 调试宏以及测试 在写代码时,不可避免需要打印提示.警告.错误等信息,且要灵活控制打印信息的级别.另外,还有可能需要使用宏来控制代码段(主要是调试代码段)是否执行.为此,本文提供一种调试宏定义方案 ...