取前K个高频词
class Word:
def __init__(self, word, cnt):
self.word = word
self.cnt = cnt
def __lt__(self, other): # True: self比other先淘汰
if self.cnt > other.cnt: # freq小的先弹出
return False
elif self.cnt < other.cnt:
return True
else:
return self.word > other.word # 字典序大的先弹出
class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
# time: nlogn
# space: n
q = []
freq = collections.Counter(words)
for word, cnt in freq.items():
heapq.heappush(q, Word(word, cnt))
if len(q) > k:
heapq.heappop(q)
res = []
while q:
res.append(heapq.heappop(q).word) # 先弹出的是频率较小的,结果要逆序
return res[::-1]
- Python collections.Counter()
参考资料1 - Python heapq模块,堆数据结构
参考资料2 - python __lt__富比较学习
参考链接3
剑指 Offer II 063. 替换单词
前缀树实现
参考链接4
参考链接5