692. Top K Frequent Words**
https://leetcode.com/problems/top-k-frequent-words/description/
题目描述
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.
Note:
- You may assume
k
is always valid,1 ≤ k ≤ number of unique elements
. - Input words contain only lowercase letters.
Follow up:
Try to solve it in O ( n log k ) O(n \log k) O(nlogk) time and O ( n ) O(n) O(n) extra space.
解题思路
使用哈希表以及优先队列来解决. 但是优先队列要自定义比较运算.
C++ 实现 1
这是两年前的代码, 使用的是最大堆. 但是用最大堆存在的问题是:
for (auto &iter : freq)
Queue.push(make_pair(iter.second, iter.first));
这一步的时间复杂度不是
O
(
n
log
k
)
O(n \log k)
O(nlogk), 因为此时最大堆中的元素不止
k
k
k 个. 所以更符合题目要求的实现应该看 C++ 实现 2
.
class Solution {
private:
struct Comp {
bool operator()(const pair<int, string> &p1, const pair<int, string> &p2) {
if (p1.first < p2.first || (p1.first == p2.first && p2 < p1))
return true;
return false;
}
};
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freq;
for (auto &s : words)
freq[s] ++;
priority_queue<pair<int, string>, vector<pair<int, string>>, Comp> Queue;
for (auto &iter : freq)
Queue.push(make_pair(iter.second, iter.first));
vector<string> res;
while (k --) {
auto ele = Queue.top();
Queue.pop();
res.push_back(ele.second);
}
return res;
}
};
C++ 实现 2
这里使用的最小堆, 始终保持堆的大小为
k
k
k, 那么最后最小堆中保存的元素是前
k
k
k 个最大值. 这里介绍一下 Comp
的实现. 由于是最小堆, 所以要满足
p.second > q.second // 返回 True
另外, 当 p.second == q.second
时, 为了让元素按照字典序排序, 那么
p.first < q.first
应该被满足.
class Solution {
private:
struct Comp {
bool operator()(const std::pair<string, int> &p,
const std::pair<string, int> &q) {
return p.second > q.second || (p.second == q.second && p.first < q.first);
}
};
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> counter;
for (auto &s : words) counter[s] ++;
priority_queue<std::pair<string, int>,
vector<std::pair<string, int>>,
Comp> Q;
for (auto &p : counter) {
Q.push(p);
if (Q.size() > k) Q.pop();
}
vector<string> res;
while (!Q.empty()) {
auto p = Q.top();
res.push_back(p.first);
Q.pop();
}
std::reverse(res.begin(), res.end());
return res;
}
};