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个最常见单词的更多相关文章

  1. [leetcode]347. Top K Frequent Elements K个最常见元素

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

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

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

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

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

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

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

  6. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

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

  8. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

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

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

随机推荐

  1. js 正则函数初级之二

    1. 小括号在正则中: 1.1 小括号:表示分组 1.2 分组之后,,每个组都有一个序号,从左到右,依次为1,2,3.......:可以使用 RegExp.$1,RegExp.$2,RegExp.$3 ...

  2. SQL Server 中系统视图sysobjects中type字段的说明

    对象类型: AF = 聚合函数 (CLR) C = CHECK 约束 D = DEFAULT(约束或独立) F = FOREIGN KEY 约束 FN = SQL 标量函数 FS = 程序集 (CLR ...

  3. weka连接mysql数据库

    一.下载并解压数据库驱动 下载地址:http://www.mysql.com/products/connector/,本文下载 mysql-connector-java-5.0.8.zip.将解压后的 ...

  4. 一个rcu回调导致的简单死锁

    在自有模块的处理中,我们设计了一个内核线程去做gc, 但同时,我们又用到了rcu,rcu中也会去抢gc的锁,由于该锁用的spin_lock,而不是spin_lock_bh,并没有关软中断,所以在rcu ...

  5. Unit 1 overview of IT Industry

    Unit 1 overview of IT IndustryConcept LearningIT Industry OutlookThe term technology commonly refers ...

  6. Kotlin语言学习笔记(1)

    fun main(args: Array<String>) { println("Hello, World!") } 基本语法 声明常量用val,声明变量用var,声明 ...

  7. jQuery图片延迟加载插件:jquery.lazyload

    ----------------------------------------------------------------------------------------------- clas ...

  8. vscode 不显示指定后缀名pyc文件

    不显示python生成的pyc文件 不显示java eclipse编辑器生成的.metadata生成的文件夹 py文件执行后会生成.pyc文件,会影响侧边栏的使用,可以通过如下设置隐藏.pyc等中间文 ...

  9. Ldap-crack-test?

    ldap #!/bin/env python import sys import ldap ldapconn = ldap.initialize('ldap://domain.adserve.com' ...

  10. ArcGIS案例学习笔记-中国2000坐标转换实例

    ArcGIS案例学习笔记-中国2000坐标转换实例 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:西安1980.中国2000.WGS84(GPS)等任意坐标系 ...