LeetCode 692. Top K Frequent Words
Medium
5302
278
Add to List
Share
Given an array of strings words and an integer k, return the k most frequent strings.
Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
Example 1:
Input: words = [“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: words = [“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.
Constraints:
1 <= words.length <= 500
1 <= words[i].length <= 10
words[i] consists of lowercase English letters.
k is in the range [1, The number of unique words[i]]
Follow-up: Could you solve it in O(n log(k)) time and O(n) extra space?
Accepted
437,840
Submissions
792,815
解法1:用unordered_map和最小堆。注意最小堆的元素是pair<string, int>,跟unordered_map的元素类型一样。
unordered_map<string, int> freqs;
struct cmp {
bool operator() (const pair<string, int> &a, const pair<string, int> &b) const {
return (a.second > b.second) || (a.second == b.second && a.first < b.first);
}
};
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
freqs.clear();
int len = words.size();
for (auto word : words) {
freqs[word]++;
}
priority_queue<pair<string, int>, vector<pair<string, int>>, cmp> minHeap;
for (auto f : freqs) {
minHeap.push(f);
if (minHeap.size() > k) minHeap.pop();
}
vector<string> res;
while (!minHeap.empty()) {
res.push_back(minHeap.top().first);
minHeap.pop();
}
reverse(res.begin(), res.end());
return res;
}
};
采用lambda表达式,上面代码可以简化一点:
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freqs;
freqs.clear();
int len = words.size();
for (auto word : words) {
freqs[word]++;
}
auto cmp = [&](const pair<string, int> &a, const pair<string, int> &b) {
return (a.second > b.second) || (a.second == b.second && a.first < b.first);
};
priority_queue<pair<string, int>, vector<pair<string, int>>, decltype(cmp)> minHeap(cmp);
for (auto f : freqs) {
minHeap.push(f);
if (minHeap.size() > k) minHeap.pop();
}
vector<string> res;
while (!minHeap.empty()) {
res.push_back(minHeap.top().first);
minHeap.pop();
}
reverse(res.begin(), res.end());
return res;
}
};
3刷:用decltype定义cmp。
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freqs;
int len = words.size();
for (auto word : words) {
freqs[word]++;
}
auto cmp = [&](string s1, string s2){
if (freqs[s1] > freqs[s2]) return true;
if (freqs[s1] == freqs[s2]) return s1 < s2;
return false;
};
priority_queue<string, vector<string>, decltype(cmp)> minHeap(cmp);
for (auto s : freqs) {
minHeap.push(s.first);
if (minHeap.size() > k) minHeap.pop();
}
vector<string> res;
while (!minHeap.empty()) {
res.push_back(minHeap.top());
minHeap.pop();
}
reverse(res.begin(), res.end());
return res;
}
};
4刷:
struct Node {
string str;
int freq;
Node(string s, int f) : str(s), freq(f) {}
bool operator < (const Node & node) const {
if (freq > node.freq) return true;
if (freq < node.freq) return false;
return str < node.str;
}
};
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
int n = words.size();
priority_queue<Node> minHeap;
unordered_map<string, int> str2Freq;
for (auto word : words) str2Freq[word]++;
int index = 0;
for (auto elem : str2Freq) {
index++;
minHeap.push(Node(elem.first, elem.second));
if (index > k) minHeap.pop();
}
vector<string> res;
while (!minHeap.empty()) {
auto top = minHeap.top();
minHeap.pop();
res.push_back(top.str);
}
reverse(res.begin(), res.end());
return res;
}
};