[LeetCode][Python3] 347. Top K Frequent Elements
2019. 1. 5. 02:01 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/top-k-frequent-elements/
My Solution :
class Solution:
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
count = {}
for n in nums:
count[n] = count.get(n, 0) + 1
bucket = [[] for _ in range(len(nums)+1)]
for n, freq in count.items():
bucket[freq].append(n)
ret = []
for n_list in bucket[::-1]:
if n_list:
ret.extend(n_list)
if len(ret) == k:
return ret
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 230. Kth Smallest Element in a BST (0) | 2019.01.15 |
---|---|
[LeetCode][Python3] 454. 4Sum II (1) | 2019.01.13 |
[LeetCode][Python3] 78. Subsets (0) | 2019.01.13 |
[LeetCode][Python3] 238. Product of Array Except Self (0) | 2019.01.12 |
[LeetCode][Python3] 94. Binary Tree Inorder Traversal (1) | 2019.01.03 |
[LeetCode][Python3] 62. Unique Paths (6) | 2018.11.15 |
[LeetCode][Python3] 55. Jump Game (0) | 2018.11.15 |
[LeetCode][Python3] 49. Group Anagrams (0) | 2018.11.14 |
최근에 달린 댓글 최근에 달린 댓글