Heapq python simple solution


  • 0
    M
            words.sort()
    
            dic = {}
            heapq = []
            res = []
    
            for word in words:
                if word not in dic:
                    dic[word] = 1
                else:
                    dic[word] += 1
    
            for word in dic:
                heappush(heapq, (-dic[word], word))
    
            for i in range(k):
                res.append(heappop(heapq)[1])
    
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.