Skip to content

Commit bdd5729

Browse files
committed
feat: solution
1 parent 9ae2050 commit bdd5729

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: List[int]
7+
"""
8+
freq = {}
9+
for n in nums:
10+
freq[n] = freq.get(n, 0) + 1
11+
12+
buckets = [[] for _ in range(len(nums) + 1)]
13+
for num, count in freq.items():
14+
buckets[count].append(num)
15+
16+
result = []
17+
for count in range(len(nums), 0, -1):
18+
for num in buckets[count]:
19+
result.append(num)
20+
if len(result) == k:
21+
return result

0 commit comments

Comments
 (0)