Hi @bee1
A Min-Heap uses the ascending priority while a Max-Heap uses the descending priority. The given solution is designed according to the min heap so we need to change the overall solution. Secondly, if you want to change it according to max heap then you can take the idea from the following code:
class Solution {
public:
struct mycompare
{
bool operator()(const pair<int, string> &p1, const pair<int, string> &p2)
{
return (p1.first<p2.first || (p1.first==p2.first && p1.second>p2.second));
};
vector<string> topKFrequent(vector<string>& words, int k) {
int n=words.size();
vector<string> res;
if(n==1 && k==1)
res.push_back(words[0]);
map<string,int> mp;
int i;
for(i=0;i<n;i++) {
if(mp[words[i]]==0)
mp[words[i]]=1;
else
mp[words[i]]++;
}
map<string,int> ::iterator it;
priority_queue<pair<int,string>, vector<pair<int, string>>, mycompare> p;
for(it=mp.begin();it!=mp.end();it++) {
p.push(make_pair(it->second,it->first));
}
while(k-- && !p.empty()) {
pair<int,string> t=p.top();
res.push_back(t.second);
p.pop();
}
return res;
}
};
Hope it will help, Thanks