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 4177cc3 commit 38b14d0Copy full SHA for 38b14d0
topKFrequent.py
@@ -0,0 +1,15 @@
1
+class Solution:
2
+ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3
+ count = {}
4
+ freq = [[] for i in range(len(nums) + 1)]
5
+ for n in nums:
6
+ count[n] = 1 + count.get(n, 0)
7
+ for n, c in count.items():
8
+ freq[c].append(n)
9
+ res = []
10
+ for i in range(len(freq)-1, 0 , -1):
11
+ for n in freq[i]:
12
+ res.append(n)
13
+ if len(res) == k:
14
+ return res[-1::-1]
15
+
0 commit comments