[leetcode]347. Top K Frequent Elements K个最常见元素
Given a non-empty array of integers, return the k most frequent elements.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2]
Example 2:
Input: nums = [1], k = 1 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
代码
/* Time: O(n) Space: O(n) */ class Solution { public List<Integer> topKFrequent(int[] nums, int k) { // freq map Map<Integer, Integer> map = new HashMap<>(); for (int num : nums) { map.put(num, map.getOrDefault(num, 0) + 1); } // bucket sort on freq List<Integer>[] buckets = new List[nums.length + 1]; for (int i : map.keySet()) { int freq = map.get(i); if (buckets[freq] == null) { buckets[freq] = new ArrayList<>(); } buckets[freq].add(i); } // gather result List<Integer> res = new ArrayList<>(); for (int i = buckets.length - 1; i >= 0; --i) { if (buckets[i] == null) continue; for (int item : buckets[i]) { res.add(item); if (k == res.size()) return res; } } return res; } }
思路
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个最常见单词完全思路一致
代码
/* Time: O(nlogk) Space: O(k) */ class Solution { public List<Integer> topKFrequent(int[] nums, int k) { // freq map Map<Integer, Integer> map = new HashMap(); for(int num : nums){ map.put(num, map.containsKey(num) ? map.get(num) + 1 : 1); } // maintain a k-size minHeap PriorityQueue <Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>((entry1, entry2) -> entry2.getValue() - entry1.getValue()); for(Map.Entry<Integer, Integer> entry : map.entrySet()){ minHeap.add(entry); } List<Integer> result = new ArrayList<>(); while(!minHeap.isEmpty() && result.size() < k){ result.add(0, minHeap.remove().getKey()); } return result; } }
[leetcode]347. Top K Frequent Elements K个最常见元素的更多相关文章
- [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 ...
- C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- [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 ...
- 347. Top K Frequent Elements (sort map)
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- Top K Frequent Elements 前K个高频元素
Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素
- 【LeetCode】347. Top K Frequent Elements 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 解题方法 字典 优先级队列 日期 题目地址:https://l ...
- 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 ...
- 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 ...
随机推荐
- Zabbix3.0版Graphtree的安装配置
Graphtrees: https://github.com/OneOaaS/graphtrees 如果是采用yum安装的zabbix-server, 则使用以下方式: # mv /usr/shar ...
- spark1.6.1 on yarn搭建部署
注:本文是建立在hadoop已经搭建完成的基础上进行的. Apache Spark是一个分布式计算框架,旨在简化运行于计算机集群上的并行程序的编写.该框架对资源调度,任务的提交.执行和跟踪,节点间的通 ...
- cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":init-param}' is expected.
第一种方案: 将 "http://java.sun.com/xml/ns/javaee" 换为 "http://java.sun.com/xml/ns/j2ee& ...
- unity admob
插件地址:https://github.com/unity-plugins/Unity-Admob 2017.04.11测试使用发现GoogleMobileAds.framework有问题,导致出现U ...
- Windows命令行打开常用界面
本文主要介绍Windows下命令行操作打开常用界面,使用方法为在DOS命令行下输入相关命令.可以减少多次操作界面.可以尝试在命令行执行下面提到的命令感受下,快捷键主要内容包括: 1.查看计算机的基本信 ...
- Java模板引擎之freemarker简介
- 学习-HTML5
@@ 学习HTML5发现对我们开发工作者来说要方便很多,它现在还在发展阶段,在未来肯定会是主流. 我们知道HTML5目的是取代HTML4.01和XHTML1.0标准,他希望能够减少互联网富应用(RIA ...
- SpringBoot(十一)过滤器和拦截器
v博客前言 在做web开发的时候,过滤器(Filter)和拦截器(Interceptor)很常见,通俗的讲,过滤器可以简单理解为“取你所想取”,忽视掉那些你不想要的东西:拦截器可以简单理解为“拒你所想 ...
- java 集成友盟推送
原文:https://blog.csdn.net/Athena072213/article/details/83414743 最近应公司业务需求需要完善友盟推送,认真看了官方文档后其实很简单,只需要细 ...
- “2017面向对象程序设计(Java)第十三周学习总结”存在问题的反馈及本周教学安排
“2017面向对象程序设计(Java)第十三周学习总结”存在问题的反馈及本周教学安排1. 图形界面事件处理技术是Java GUI编程核心技术,要求同学们掌握其基本原理和基本编程模型:2. 本周四理论课 ...