We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9ae2050 commit bdd5729Copy full SHA for bdd5729
top-k-frequent-elements/jun-brro.py
@@ -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