Problem :

https://leetcode.com/problems/top-k-frequent-words/


My Solution :

class Solution:
def topKFrequent(self, words, k):
counter = {}
for word in words:
counter[word] = counter.get(word, 0) + 1
bucket = [[] for _ in range(len(words)+1)]
for word, count in counter.items():
bucket[count].append(word)
ans = []
for word_list in bucket[::-1]:
if word_list:
for word in sorted(word_list):
ans.append(word)
k -= 1
if k == 0:
return ans


Comment :

지난번과 거의 비슷한 문제라 비슷한 방식으로 접근하였다.


2019/01/05 - [프로그래밍/LeetCode] - [LeetCode][Python3] 347. Top K Frequent Elements