LeetCode #692 - Top K Frequent Words - Priority Queue / Hash-map

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值