-
Notifications
You must be signed in to change notification settings - Fork 1
/
top-k-frequent-words.py
47 lines (39 loc) · 1.24 KB
/
top-k-frequent-words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding=utf-8
from collections import Counter
"""
692. Top K Frequent Words
"""
class Solution(object):
def topKFrequent(self, words, k):
"""
:type words: List[str]
:type k: int
:rtype: List[str]
"""
count = Counter(words)
candidates = count.keys()
candidates.sort(key=lambda w: (-count[w], w))
return candidates[:k]
def topKFrequent1(self, words, k):
import heapq
count = Counter(words)
min_heap = []
# 用size为k的最小堆,把(fre, word)放入堆里,最小fre出堆,最后剩下的k个就是频率最大的
for word, fre in count.items():
heapq.heappush(min_heap, (Word(word, fre), word))
if len(min_heap) > k:
heapq.heappop(min_heap)
res = []
while min_heap:
res.append(heapq.heappop(min_heap)[1])
return res[::-1]
class Word(object):
def __init__(self, word, fre):
self.word = word
self.fre = fre
def __lt__(self, other):
if self.fre == other.fre:
return self.word > other.word
return self.fre < other.fre
def __eq__(self, other):
return self.fre == other.fre and self.word == other.word