LeetCode #692 - Top K Frequent Words
Description
Given a non-empty list of words, return the k most frequent elements, with Time Complexity of O(nlogk) and Space Complexity of O(n).
对于一串非空的单词数组,找出它的前k个最常出现的单词,要求时间复杂度为O(nlogk),空间复杂度为O(n)。
Sample I/O
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.
Key solution
本题关键是数据结构:
- hash-map (dictionary)
- maximum priority queue of k capabilities
首先用字典保存单词出现次数,再利用这个字典构建一个大小为k的降序队列。
Implementation in Python
class k_PQ:
""" maximum priority queue of k capabilities"""
def __init__(self, k):
self.queue = []
self.k = k
def enqueue(self, val):
# append if no item in k_PQ
if self.queue == []:
return self.queue.append(val)
begin = 0
end = len(self.queue)-1
b_lr = False
# do binary-search of time complexity O(logk)
while begin <= end:
mid = int((begin+end)/2)
ele = self.queue[mid]
if val[1] > ele[1] or (val[1] == ele[1] and val[0] < ele[0]):
b_lr = True
end = mid - 1
elif val[1] < ele[1] or (val[1] == ele[1] and val[0] > ele[0]):
begin = mid + 1
b_lr = False
self.queue.insert(mid if b_lr else mid+1, val)
# pop the last word if overflowed
if len(self.queue) > self.k:
self.dequeue()
def dequeue(self):
self.queue.pop()
class Solution:
def topKFrequent(self, words, k):
""" space complexity of O(n) + O(k) = O(n) """
records = {}
pq = k_PQ(k)
""" time complexity of O(n) + O(nlogk) = O(nlogk) """
# record frequences, time complexity of O(n)
for word in words:
records[word] = [word, records[word][1] + 1 if word in records else 1]
# build a k min-heap, time complexity of O(nlogk)
for i in records.values():
pq.enqueue(i)
return list(i[0] for i in pq.queue)
转载请注明出处@汤pp