[LeetCode][Python3] 692. Top K Frequent Words
2019. 9. 3. 00:45 |
프로그래밍/LeetCode
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
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 406. Queue Reconstruction by Height (0) | 2019.09.23 |
---|---|
[LeetCode][Python3] 90. Subsets II (0) | 2019.09.17 |
[LeetCode][Python3] 739. Daily Temperatures (0) | 2019.09.12 |
[LeetCode][Python3] 401. Binary Watch (0) | 2019.09.05 |
[LeetCode][Python3] 89. Gray Code (0) | 2019.09.03 |
[LeetCode][Python3] 357. Count Numbers with Unique Digits (0) | 2019.08.29 |
[LeetCode][Python3] 996. Number of Squareful Arrays (0) | 2019.08.28 |
[LeetCode][Python3] 174. Dungeon Game (0) | 2019.08.28 |
최근에 달린 댓글 최근에 달린 댓글