难度: Medium
原题连接
内容描述
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) time and O(n) extra space.
思路 1 - 时间复杂度: O(NlgN)- 空间复杂度: O(N)******
beats 28.33%
class Solution:
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
lookup = collections.Counter(words)
words = list(set(words)) # list(lookup.keys()) 也可以
words.sort(key = lambda w: (-lookup[w], w))
return words[:k]
可以写成一行
class Solution:
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
return [w for w, v in sorted(collections.Counter(words).items(), key = lambda x: (-x[1], x[0])) [:k]]
思路 2 - 时间复杂度: O(Nlgk)- 空间复杂度: O(N)******
heapq + topk, 可以实现Nlgk
class WordFreq:
def __init__(self, freq, word):
self.freq = freq
self.word = word
def __lt__(self, other):
if self.freq != other.freq:
# return self.freq.__lt__(other.freq)
return self.freq < other.freq
else:
# return self.word.__gt__(other.word)
return self.word > other.word
class Solution:
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
counts = collections.Counter(words)
topk = []
heapq.heapify(topk)
for word, freq in counts.items():
heapq.heappush(topk, WordFreq(freq, word))
# solution 2:
# heapq.heappush(topk, (WordFreq(freq, word), word))
if len(topk) > k:
heapq.heappop(topk)
res = []
for _ in range(k):
res.append(heapq.heappop(topk).word)
# solution 2:
# res.append(heapq.heappop(topk)[1])
return res[::-1]