题目:

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. 力扣414(java)-第三大的数(简单)

    题目: 给你一个非空数组,返回此数组中 第三大的数 .如果不存在,则返回数组中最大的数. 示例 1: 输入:[3, 2, 1]输出:1解释:第三大的数是 1 .示例 2: 输入:[1, 2]输出:2解 ...

  2. HarmonyOS NEXT应用开发之深色模式适配

    介绍 本示例介绍在开发应用以适应深色模式时,对于深色和浅色模式的适配方案,采取了多种策略如下: 固定属性适配:对于部分组件的颜色属性,如背景色或字体颜色,若保持不变,可直接设定固定色值或引用固定的资源 ...

  3. DNS高可用设计--软件高可用

    DNS是网络的基础服务,网络上的各种应用对DNS的依赖性很高.DNS的稳定,直接决定了上层应用服务的稳定.那如何保障DNS服务的高可用呢?我们先来看下高可用的概念: 高可用 高可用(High avai ...

  4. 龙蜥正式开源 SysOM:百万级实战经验打造!一站式运维管理平台 | 龙蜥技术

    ​简介:SysOM集监控.告警.诊断.修复.安全能力于一体的操作系统运维平台. ​ 文/系统运维 SIG 如果你被突如其来的 OOPS 和满屏奇怪的函数弄得满头问号?机器内存明明很大,却申请不出来内存 ...

  5. MaxCompute湖仓一体介绍

    ​简介:本篇内容分享了MaxCompute湖仓一体介绍. 分享人:孟硕 阿里云 MaxCompute产品专家 视频链接:数据智能实战营-北京站 专题回顾 正文: 本篇内容将通过两个部分来介绍MaxCo ...

  6. 快手基于RocketMQ的在线消息系统建设实践

    简介: 快手需要建设一个主要面向在线业务的消息系统作为 Kafka 的补充,低延迟.高并发.高可用.高可靠的分布式消息中间件 RocketMQ 正是我们所需的. 作者:黄理 黄理,10多年软件开发和架 ...

  7. 几种Java常用序列化框架的选型与对比

    简介: 序列化与反序列化是我们日常数据持久化和网络传输中经常使用的技术,但是目前各种序列化框架让人眼花缭乱,不清楚什么场景到底采用哪种序列化框架.本文会将业界开源的序列化框架进行对比测试,分别从通用性 ...

  8. WPF 基于 Azure 的认知服务 情绪分析 语言检测 关键短语提取

    本文主要是来安利大家基于 Azure 的认知服务,主要是文本认知服务,可以做到分析输入文本的情绪,以及判断当前输入文本所属语言等功能 本文分为两个部分 ,一个就是在 Azure 上的配置,另一个就是 ...

  9. 2019-8-31-C#-转换类型和字符串

    title author date CreateTime categories C# 转换类型和字符串 lindexi 2019-08-31 16:55:58 +0800 2018-2-13 17:2 ...

  10. django-rest-framework框架(一)

    1.Web开发模式 # web开发模式 #前后端混合开发(前后端不分离):返回的是html的内容,需要写模板 #前后端分离:只专注于写后端接口,返回json,xml格式数据 # xml格式 <x ...