LeetCode347:返回频率前K高的元素,基于优先队列实现
package com.lt.datastructure.MaxHeap; import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap; import com.lt.datastructure.Queue.PriorityQueue;
/**
LeetCode347
给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
示例 1:
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
说明:
你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。
你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。 频次: 用map 复杂度优于O(nlogn):优先队列 频次越低,优先于越高
1 TreeMap存储,键存数组的值,值存数组的值频次
2 新建Freq类,成员属性是e,freq,实现Comparable,重写CompareTo,相反地,频次小的优先级高,返回1
3 优先队列存储Freq,遍历map,如果没存满k个,继续入队,如果存满了,将队首元素和新元素的频次比较,优先级高的(频次低)出队
4 用LinkedList存储优先队列中的元素,作为结果输出
*/
public class Solution{
private class Freq implements Comparable<Freq>{
public int e,freq;
public Freq(int e, int freq) {
this.e = e;
this.freq = freq;
}
@Override
public int compareTo(Freq another) {
if(this.freq < another.freq){
return 1;
}else if(this.freq > another.freq){
return -1;
}else{
return 0;
}
} }
public List<Integer> topKFrequent(int[] nums, int k) { //映射存储元素和频次
TreeMap<Integer,Integer> map = new TreeMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num, map.get(num)+1);
}else{
map.put(num, 1);
}
}
//优先队列存储前k个频次最高的元素
PriorityQueue<Freq> pq = new PriorityQueue<>();
for(int key : map.keySet()){
//没存满,继续存
if(pq.getSize()<k){
pq.enqueue(new Freq(key,map.get(key)));
//存满了,比较次数,次数低的优先级高,出队,频次高的入队
}else if(map.get(key)>pq.getFront().freq){
pq.dequeue();
pq.enqueue(new Freq(key,map.get(key)));
}
}
//将优先队列的元素存于链表并作为结果输出
LinkedList<Integer> res = new LinkedList<>();
while(!pq.isEmpty()){
res.add(pq.dequeue().e);
}
return res;
}
}
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.TreeMap; public class Solution { private class Freq implements Comparable<Freq>{ //元素,频次
public int e,freq;
public Freq(int e , int freq) {
this.e = e;
this.freq = freq;
} @Override
public int compareTo(Freq another) {
if(this.freq < another.freq){
return -1;
}
else if(this.freq > another.freq){
return 1;
}else{
return 0;
}
} } public List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> map = new TreeMap<>();
for (int i : nums) {
if(map.containsKey(i)){
map.put(i, map.get(i) + 1);
}else{
map.put(i, 0);
}
} //最小堆,频次最高的优先出队
PriorityQueue<Freq> queue = new PriorityQueue<>();
for(int key : map.keySet()){
if(queue.size() < k){
queue.add(new Freq(key, map.get(key)));
}
else if(map.get(key) > queue.peek().freq){
queue.remove();
queue.add(new Freq(key, map.get(key)));
}
} LinkedList<Integer> res = new LinkedList<>();
while(!queue.isEmpty()){
res.add(queue.remove().e);
}
return res;
}
}
LeetCode347:返回频率前K高的元素,基于优先队列实现的更多相关文章
- LeetCode347——优先队列解决查询前k高频率数字问题
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 例如, 给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]. 注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...
- 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- Leetcode 347.前K个高频元素 By Python
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = [1], ...
- 【LeetCode题解】347_前K个高频元素(Top-K-Frequent-Elements)
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket ...
- LeetCode:前K个高频元素【347】
LeetCode:前K个高频元素[347] 题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [ ...
- 代码题(3)— 最小的k个数、数组中的第K个最大元素、前K个高频元素
1.题目:输入n个整数,找出其中最小的K个数. 例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 快排思路(掌握): class Solution { public ...
- 347 Top K Frequent Elements 前K个高频元素
给定一个非空的整数数组,返回其中出现频率前 k 高的元素.例如,给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2].注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- 347. 前K个高频元素
题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = ...
随机推荐
- docker+hexo 搭建博客
前提 Linux服务器 保证自己服务器上的端口对外开放,即设置相应的防火墙规则 安装好hexo 安装:npm install hexo-cli -g 初始化搭建:npm init myBlog,myB ...
- tf.app.run()的作用
tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- 「JSOI2014」歌剧表演
「JSOI2014」歌剧表演 传送门 没想到吧我半夜切的 这道题应该算是 \(\text{JSOI2014}\) 里面比较简单的吧... 考虑用集合关系来表示分辨关系,具体地说就是我们把所有演员分成若 ...
- laravel 排除csrf验证
中(*排除所有路由)
- if,while,for循环
目录 if条件 while循环 for循环 拓展知识点 if条件 if 条件: code elif 条件: code else: code # 三元运算符 x = 10 y = 20 print(y ...
- 【原】python异步学习
https://www.liaoxuefeng.com/wiki/1016959663602400/1017959540289152 https://www.jianshu.com/p/b5e347b ...
- iOS 开发之 开发一款自己的美颜相机
以前在公司做项目时很少遇到对相机.图片进行处理的(非公司业务),只是偶尔上传,裁剪,预览下.近期自己准备写个相机应用,把图片处理的这些技术细节整理下.包含美颜相机,图片美化,简单拼图,艺术拼图等主要模 ...
- 执行SQL时出现: ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
注意: 采用navicat新建数据库时,需要将编码方式设置为,字符集:utf8 -- UTF-8 Unicode ,排序规则:utf8_general_ci 在运行sql语句时,出现以下问题: [Er ...
- A letter for NW RDMA configuration
Dear : If you have to use EMC NW NDMA to backup oracle database and want to see what happen when bac ...