我正在尝试解决来自Leetcode网站的问题 - 找到前K个高频单词。
给定一个非空单词列表,返回出现频率最高的k个元素。您的答案应按频率从高到低排序。如果两个单词具有相同的频率,则较低字母顺序的单词先出现。例如:如果输入为:["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"],k = 4,则输出应为:["the", "is", "sunny", "day"]。
其中一个点赞的解决方案如下:
给定一个非空单词列表,返回出现频率最高的k个元素。您的答案应按频率从高到低排序。如果两个单词具有相同的频率,则较低字母顺序的单词先出现。例如:如果输入为:["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"],k = 4,则输出应为:["the", "is", "sunny", "day"]。
其中一个点赞的解决方案如下:
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string,int> dict;
for(const string& s:words) dict[s]++;
priority_queue<pair<string,int>, vector<pair<string,int>>, Comp> pq;
for(const auto& pa:dict) {
pq.push(pa);
if(pq.size()>k) pq.pop();
}
vector<string> result;
while(!pq.empty()) {
result.push_back(pq.top().first);
pq.pop();
}
reverse(result.begin(),result.end());
return result;
}
private:
struct Comp {
Comp() {}
~Comp() {}
bool operator()(const pair<string,int>& a, const pair<string,int>& b) {
return a.second>b.second || (a.second==b.second && a.first<b.first);
}
};
};
我正在努力理解它,并有一些问题:
- 当
pq.size()>k
时,我们执行pop()
- 这不正确,因为在这种情况下,我们会丢失最高频率的元素。我认为是因为按照比较器,具有更高频率(或在相等频率的情况下字典序更小)的元素被插入到优先队列的顶部。 - 对于优先队列,当我们实现自己的比较器时,我们必须传递第二个参数(表示要使用的容器),但在使用默认比较器时不需要 - 为什么?我的意思是,不能根据我将存储的值的类型(第一个参数,在本例中为
pair<string,int>
)自动推断出默认容器类型吗? - 在
pq.push(pa);
的情况下,pa
的确切类型是什么?我想知道的原因是,pq
包含vector<pair<string,int>>
,但dict
只包含string
(映射到它们在int
中的频率)。如何使用auto
自动将string
键映射到其int
频率以插入优先队列中?
非常抱歉问题这么长。感谢您的帮助。
std::pair<string,int>
,而是std::pair<const string,int>
;> - Fureeishreturn a.second>b.second || (a.second==b.second && a.first<b.first);
,那么它不是按照出现频率最高的元素排在最上面吗?你能否详细解释一下?这对我的理解会非常有帮助。 - user6490375