当前位置:网站首页>Leetcode 347. top k high frequency elements
Leetcode 347. top k high frequency elements
2022-07-26 06:09:00 【LuZhouShiLi】
Leetcode 347. front K High frequency elements
subject
Give you an array of integers nums And an integer k , Please return to the frequency before k High element . You can press In any order Return to the answer .
Ideas
- First, use the hash table to count the number of occurrences of each element
- Define a small root heap , It's actually a priority queue , The length is k, Send all frequencies into the stack , Build a small root heap , If the length is greater than k, Straight out of the team , Every time out of the team is the smallest element
Code
class Solution {
public:
class mycomparison{
public:
bool operator()(const pair<int,int>& lhs,const pair<int,int>& rhs){
return lhs.second > rhs.second;
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
// First count the occurrence frequency of elements
unordered_map<int,int> map;// Use a hash table to store the number of occurrences of each element
for(int i = 0; i < nums.size(); i++)
{
map[nums[i]]++;// Add one to the number of times the element appears
}
// Sort frequencies
// Define a small root heap The size is k
priority_queue<pair<int,int>,vector<pair<int,int>>,mycomparison> pri_que;
// Use a fixed size of k Small roots
for(unordered_map<int,int>::iterator it = map.begin();it != map.end(); it++)
{
pri_que.push(*it);
if(pri_que.size() > k)
{
// If the heap size is greater than k Queue Popup Make sure the heap size is always K Maintain a priority queue
pri_que.pop();
}
}
// Before finding out k Elements , The smallest element pops up first in the small root heap , So output in reverse order
vector<int> result(k);
for(int i = k - 1; i >= 0; i--)
{
result[i] = pri_que.top().first;// Get the first element of the queue
pri_que.pop();
}
return result;
}
};
边栏推荐
- Byte interview question - judge whether a tree is a balanced binary tree
- Docking wechat payment (II) unified order API
- Convolutional neural network (III) - target detection
- 语法泛化三种可行方案介绍
- "Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
- 【Day_06 0423】不要二
- Mysql45 speak in simple terms index
- Is the transaction in mysql45 isolated or not?
- 对接微信支付(二)统一下单API
- Embedded sharing collection 14
猜你喜欢

Matlab 向量与矩阵

基于消防GIS系统的智慧消防应用

递归函数中 有两个递归入口的时间复杂度

2022年下半年系统集成项目管理工程师(软考中级)报名条件

知识沉淀一:架构师是做什么?解决了什么问题

ETCD数据库源码分析——Cluster membership changes日志

时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)

金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
随机推荐
Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Mysql45 talking about global lock, table lock and row lock
Webapi collation
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
Widget is everything, widget introduction
How can programmers improve mental internal friction?
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
Calling mode and execution sequence of JS
Jincang database kingbasees SQL language reference manual (5. Operators)
Viewing the technology stack of distributed system from the crash report of station B
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
Embedded sharing collection 15
对接微信支付(二)统一下单API
卸载手机自带APP的操作步骤
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Practice operation and maintenance knowledge accumulation
【Day_03 0420】数组中出现次数超过一半的数字
Flex layout