Given a non-empty array of integers, return the k most frequent elements.

Example 1:

  1. Input: nums = [1,1,1,2,2,3], k = 2
  2. Output: [1,2]

Example 2:

  1. Input: nums = [1], k = 1
  2. Output: [1]

Note:

  • You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  • Your algorithm's time complexity must be better than O(n log n), where n is the array's size.

题目

给定数组,求其中出现频率最高的K个元素。

思路

bucket sort

代码

  1. /*
  2. Time: O(n)
  3. Space: O(n)
  4. */
  5. class Solution {
  6. public List<Integer> topKFrequent(int[] nums, int k) {
  7. // freq map
  8. Map<Integer, Integer> map = new HashMap<>();
  9. for (int num : nums) {
  10. map.put(num, map.getOrDefault(num, 0) + 1);
  11. }
  12. // bucket sort on freq
  13. List<Integer>[] buckets = new List[nums.length + 1];
  14. for (int i : map.keySet()) {
  15. int freq = map.get(i);
  16. if (buckets[freq] == null) {
  17. buckets[freq] = new ArrayList<>();
  18. }
  19. buckets[freq].add(i);
  20. }
  21. // gather result
  22. List<Integer> res = new ArrayList<>();
  23. for (int i = buckets.length - 1; i >= 0; --i) {
  24. if (buckets[i] == null) continue;
  25. for (int item : buckets[i]) {
  26. res.add(item);
  27.  
  28. if (k == res.size()) return res;
  29. }
  30. }
  31. return res;
  32. }
  33. }

思路

priorityqueue to track Top K Frequent Elements

1. Using HashMap and PriorityQueue
2. Build a HashMap to get the item frequency
3. PriorityQueue (minHeap) 
4.  If the PQ size > k, then we pop the lowest value in the PQ

[leetcode]692. Top K Frequent Words K个最常见单词完全思路一致

代码

  1. /*
  2. Time: O(nlogk)
  3. Space: O(k)
  4. */
  5. class Solution {
  6. public List<Integer> topKFrequent(int[] nums, int k) {
  7. // freq map
  8. Map<Integer, Integer> map = new HashMap();
  9. for(int num : nums){
  10. map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1);
  11. }
  12. // maintain a k-size minHeap
  13. PriorityQueue <Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>((entry1, entry2) -> entry2.getValue() - entry1.getValue());
  14.  
  15. for(Map.Entry<Integer, Integer> entry : map.entrySet()){
  16. minHeap.add(entry);
  17. }
  18.  
  19. List<Integer> result = new ArrayList<>();
  20. while(!minHeap.isEmpty() && result.size() < k){
  21. result.add(0, minHeap.remove().getKey());
  22. }
  23. return result;
  24. }
  25. }

[leetcode]347. Top K Frequent Elements K个最常见元素的更多相关文章

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

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

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

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

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

  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. 347. Top K Frequent Elements (sort map)

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

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

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

  7. 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...

  8. LeetCode 【347. Top K Frequent Elements】

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  9. Leetcode 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

随机推荐

  1. Raft 为什么是更易理解的分布式一致性算法(转)

    一致性问题可以算是分布式领域的一个圣殿级问题了,关于它的研究可以回溯到几十年前. 拜占庭将军问题 Leslie Lamport 在三十多年前发表的论文<拜占庭将军问题>(参考[1]). 拜 ...

  2. 【358】GitHub 上面文件夹下载方法

    参考:https://www.bilibili.com/read/cv210500/ 参考:https://www.jianshu.com/p/743ecc20ffb2 软件下载:Downloads ...

  3. Excel导入oracle的几种方法

    http://www.jb51.net/list/list_154_1.htm 方法一.使用SQL*Loader这个是用的较多的方法,前提必须oracle数据中目的表已经存在.大体步骤如下:1.将ex ...

  4. 解决运行wamp提示“MSVCR110.dll”丢失的问题!

    我在Windows系统上安装wampserver2.5 64位,安装到最后,总是提示丢失msvcr110.dll 解决办法: 到这个网站下载一个Visual C++ Redistributable f ...

  5. List,Set,Map集合的遍历方法

    List的三种实现:ArrayList(数组)  LinkedList(链表)  Vector(线程安全) List集合遍历方法: List<String> list = new Arra ...

  6. 使用Python抓取猫眼近10万条评论并分析

    <一出好戏>讲述人性,使用Python抓取猫眼近10万条评论并分析,一起揭秘“这出好戏”到底如何? 黄渤首次导演的电影<一出好戏>自8月10日在全国上映,至今已有10天,其主演 ...

  7. SQL server 2012完全删除

    第一步,在控制面板里面找到程序——卸载程序这一项,打开之后就会是这样的了 第二步,经过第一步打开卸载程序后,在里面找到Microsoft SQLserver 2012 (64-bit)这一项,可以通过 ...

  8. tomcat 启动脚本

    #!/bin/bash##干掉运行中的tomcat  results=把结果赋值给变量,可以保证命令上条执行完再执行下一条#也可以用这句简单的代码按进程名kill: ps -ef | grep 进程名 ...

  9. 解决iframe在iphone不兼容的问题

    <div class="scroll-wrapper"> <iframe src="地址"></iframe> </d ...

  10. 终止执行js的方法

    (一)在function里面 (1)return;(2)return false; (二)非function方法里面 alert("before error.");throw Sy ...