当前位置:网站首页>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;
}
};
边栏推荐
- YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
- The time complexity of two recursive entries in a recursive function
- 逆序打印链表
- Operating steps for uninstalling the mobile app
- redis 哨兵集群搭建
- Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
- Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
- Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
- Knowledge precipitation I: what does an architect do? What problems have been solved
- 【Day_07 0425】Fibonacci数列
猜你喜欢

Widget is everything, widget introduction

Embedded sharing collection 15

Niuke network: TOPK problem of additive sum between two ordinal groups

Database SQL language practice

Operating steps for uninstalling the mobile app

Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network

Mysql45 talking about infrastructure: how is an SQL query executed?

漫谈软件缺陷管理的实践

Leetcode:336. palindrome pair

Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
随机推荐
【Day04_0421】C语言选择题
【Day05_0422】C语言选择题
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
Xiao He shows his sharp corners and says hello to flutter app
Servlet无法直接获取request请求中的JSON格式数据
英语句式参考纯享版 - 状语从句
Mysql45 talking about infrastructure: how is an SQL query executed?
程序员如何改善精神内耗?
金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)
Realize channel routing based on policy mode
[free and easy to use] holiday query interface
Modifiers should be declared in the correct order
【Day_04 0421】计算糖果
二叉树的前中后序遍历——本质(每个节点都是“根”节点)
Byte interview question - judge whether a tree is a balanced binary tree
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
2022年下半年系统集成项目管理工程师(软考中级)报名条件
"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree
Latex merges multiple rows and columns of a table at the same time
Calling mode and execution sequence of JS