首页 > 其他分享 >优先队列--著名的TopK问题(最小堆的使用)

优先队列--著名的TopK问题(最小堆的使用)

时间:2022-10-26 21:07:29浏览次数:48  
标签:return -- a1 队列 a2 TopK words pair first


692. Top K Frequent Words

Medium

77671FavoriteShare

Given a non-empty list of words, return the k most frequent elements.

Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.

Example 1:

Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.

 

Example 2:

Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.
class Solution {

public:
inline bool Cmp1(const pair<int,string>& a1,const pair<int,string>& a2){
if(a1.first > a2.first){
return true;
}
if(a1.first == a2.first && a1.second < a2.second){
return true;
}
return false;
}
struct Cmp{
bool operator ()(const pair<int,string>& a1,const pair<int,string>& a2){
if(a1.first > a2.first){
return true;
}
if((a1.first == a2.first) && (a1.second < a2.second)){
return true;
}
return false;
}
};
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string,int> HashMap;
priority_queue<pair<int,string>,vector<pair<int,string>>,Cmp> Q;
vector<string> Result;
int cnt = 0;
for(auto word : words){
HashMap[word] ++;
}
for(auto i : HashMap){
pair<int,string> tmp= make_pair(i.second,i.first);
if(cnt < k){
Q.push(tmp);
cnt++;
}
else{
if(Cmp1(tmp,Q.top())){
Q.push(tmp);
Q.pop();
}
}
}
while(!Q.empty()){
pair<int,string> tmp = Q.top();
Result.push_back(tmp.second);
Q.pop();
}
reverse(Result.begin(),Result.end());
return Result;
}
};

 

标签:return,--,a1,队列,a2,TopK,words,pair,first
From: https://blog.51cto.com/u_13121994/5798504

相关文章

  • 一个字符串用空格作为分隔符,可以用while(cin>>Input)进行输入
    题目描述给定一个句子(只包含字母和空格),将句子中的单词位置反转,单词用空格分割,单词之间只有一个空格,前后没有空格。比如:(1)“helloxiaomi”->“mixiaohello”输入描......
  • 从小到大排序
    题目描述六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i](即w[j]>=h[i]),他才会上去表演节目。老师的......
  • 字典树--字典树题母
    648. ReplaceWordsMedium457110FavoriteShareInEnglish,wehaveaconceptcalled ​​root​​​,whichcanbefollowedbysomeotherwordstoformanotherlong......
  • 连线问题(数学题)
    题目描述某一天,Alice比较无聊,于是她为自己发明了一个游戏玩。首先她在纸上画了一个圆,然后从这个圆的圆弧上均匀地取出n个点,这n个点将圆n等分。接下来,Alice每次从这......
  • 倒序排序求次数平方和最大值
    题目描述有一种有趣的字符串价值计算方式:统计字符串中每种字符出现的次数,然后求所有字符次数的平方和作为字符串的价值例如:字符串"abacaba",里面包括4个'a',2个'b',1个......
  • 贪心找零钱
    题目描述楚乔、宇文玥和燕洵在日本旅行,经过了几天的游玩之后,钱包里出现了大量硬币,楚乔决定用钱包里的硬币为宇文玥和燕洵在自动贩卖机买水。楚乔的钱包里有1元、5元、10元、......
  • 有时间再研究吧
    #include<bits/stdc++.h>usingnamespacestd;structpt{intx;intpos;pt(inta,intb):x(a),pos(b){}};structcmp{//重写比较函数booloperator......
  • 利用map对数组中的元素及其下标进行存储
    题目描述小摩有一个N个数的数组,他想将数组从小到大排好序,但是萌萌的小摩只会下面这个操作:任取数组中的一个数然后将它放置在数组的最后一个位置。问最少操作多少次可以使得......
  • Java8新特性-函数式接口
    一、函数式接口1.1简介首先的是一个接口接口内有且只有一个抽象方法为防止破坏函数式接口,最好是在接口上使用@FunctionalInterface注解修饰定义一个函数接口pa......
  • 根据股票code去重然后取每只股票最新的数据
    List<CoverLog>list=collect2.stream().filter(distinctByKey(CoverLog::getStockCode)).collect(Collectors.toList());Map<String,CoverLog>collect4=collect2.st......