692. Top K Frequent Words
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(nlogk) time and O(n) extra space.
Thoughts:
- Custom the sorting: first sort the count in descending order, then sort the name in ascending order
- O(nlogk) time and O(n) extra space: using a heap to keep top k words in the heap
Code: Sorting: T: O(nlogn); S: O(n)
class Solution(object):
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
d = collections.defaultdict(int)
for word in words:
d[str(word)] += 1
# alternative ways to do that
# d = collections.Counter(words)
return [i[0] for i in sorted(d.items(), key = lambda x : (-x[1], x[0]))[:k]] # top K
# return [w for w, v in sorted(collections.Counter(words).items(), key = lambda x: (-x[1], x[0])) [:k]]
Code: Heap
class Solution(object):
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
d = collections.defaultdict(int)
for word in words:
d[word] +=1
queue = []
for word, count in d.items():
heapq.heappush(queue, Element(count, word))
if len(queue) > k:
heapq.heappop(queue)
return [heapq.heappop(queue).word for _ in range(k)][::-1]
class Element(object):
def __init__(self, count, word):
self.word = word
self.count = count
def __lt__(self, other):
if self.count == other.count:
return self.word > other.word
return self.count < other.count
def __eq__ (self, other):
return self.count == other.count and self.word == other.word
Code: Heap Java:
class Solution {
public List<String> topKFrequent(String[] words, int k) {
List<String> res = new LinkedList<>(); // add front from min heap
Map<String, Integer> map = new HashMap<>();
for(int i = 0; i < words.length; i++){
if(map.containsKey(words[i])){
map.put(words[i], map.get(words[i])+ 1);
}
else{
map.put(words[i], 1);
}
}
// max heap
PriorityQueue<Map.Entry<String, Integer>>pq = new PriorityQueue<>(
(a,b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()) :a.getValue() - b.getValue()
);
for (Map.Entry<String, Integer> entry: map.entrySet()){
pq.offer(entry);
if(pq.size()> k) pq.poll();
}
while(!pq.isEmpty()){
res.add(0, pq.poll().getKey()); // only record the name
}
return res;
}
}
Code: Heap C++
class Solution {
public:
vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> freq;
for(auto w : words){
freq[w]++;
}
auto comp = [&](const pair<string,int>& a, const pair<string,int>& b) {
return a.second > b.second || (a.second == b.second && a.first < b.first);
};
// typedef priority_queue<pair<string,int>, vector<pair<string,int>>, decltype(comp)> my_pq_t;
priority_queue<pair<string,int>, vector<pair<string,int>>, decltype(comp)> pq(comp); // element, container, comparison
for(auto w : freq ){
pq.emplace(w.first, w.second);
if(pq.size()>k) pq.pop();
}
vector<string> res;
while(!pq.empty()){
res.insert(res.begin(), pq.top().first);
pq.pop();
}
return res;
}
};