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:
Follow up:
1 import collections 2 import heapq 3 4 class Element(object): 5 def __init__(self, word, freq): 6 self.word = word 7 self.freq = freq 8 9 def __lt__(self, other): 10 if self.freq != other.freq: 11 return self.freq < other.freq 12 return other.word < self.word 13 14 class Solution(object): 15 def topKFrequent(self, words, k): 16 """ 17 :type words: List[str] 18 :type k: int 19 :rtype: List[str] 20 """ 21 my_dict = {} 22 for word in words: 23 if word in my_dict: 24 my_dict[word] += 1 25 else: 26 my_dict[word] = 1 27 28 freqs = [] 29 for word, count in my_dict.items(): 30 heapq.heappush(freqs, (Element(word, count))) 31 if len(freqs) > k: 32 heapq.heappop(freqs) 33 res = [] 34 for _ in range(k): 35 res.append(heapq.heappop(freqs).word) 36 res.reverse() 37 return res 38 39
[LC] 692. Top K Frequent Words
原文:https://www.cnblogs.com/xuanlu/p/11577480.html