给一个单词列表,求出这个列表中出现频次最高的K个单词。
样例
样例 1:
输入:
[
"yes", "lint", "code",
"yes", "code", "baby",
"you", "baby", "chrome",
"safari", "lint", "code",
"body", "lint", "code"
]
k = 3
输出: ["code", "lint", "baby"]
样例 2:
输入:
[
"yes", "lint", "code",
"yes", "code", "baby",
"you", "baby", "chrome",
"safari", "lint", "code",
"body", "lint", "code"
]
k = 4
输出: ["code", "lint", "baby", "yes"]
挑战
用 O(n log k)的时间和 O(n) 的额外空间完成。
注意事项
你需要按照单词的词频排序后输出,越高频的词排在越前面。如果两个单词出现的次数相同,则词典序小的排在前面。
bool cmp(pair<string, int> a, pair<string, int> b)
{
if(a.second > b.second)
return true;
if(a.second < b.second)
return false;
if(a.first < b.first)
return true;
return false;
}
class Solution {
public:
/**
* @param words: an array of string
* @param k: An integer
* @return: an array of string
*/
vector<string> topKFrequentWords(vector<string> &words, int k) {
// write your code here
vector<string> ret;
if(words.size() == 0)
return ret;
if(k == 0)
return ret;
unordered_map<string,int> mymap;
for(int i = 0; i < words.size(); i++)
{
mymap[words[i]]++;
}
vector<pair<string, int>> vc;
for(auto mit=mymap.begin(); mit!=mymap.end(); mit++)
{
vc.push_back(pair<string, int>(mit->first, mit->second));
}
sort(vc.begin(), vc.end(), cmp);
//sort(vc.begin(), vc.end(), cmp1);
for(auto vit=vc.begin(); vit!=vc.end(); vit++)
{
// cout<<vit->first<<" "<<vit->second<<endl;
ret.push_back(vit->first);
if(ret.size() == k)
break;
}
return ret;
}
};