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 7123351 commit 903bc2eCopy full SHA for 903bc2e
top-k-frequent-elements/yayyz.py
@@ -1,7 +1,21 @@
1
+# Solution 1: using Counter, heapq
2
from collections import Counter
3
import heapq
4
5
class Solution:
6
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
- count_dict = Counter(nums)
7
+ count_dict = Counter(nums)
8
return heapq.nlargest(k, count_dict.keys(), key=count_dict.get)
9
+
10
+# Solution 2: create dict, use sorted function
11
+# class Solution:
12
+# def topKFrequent(self, nums: List[int], k: int) -> List[int]:
13
+# freq_dict = {}
14
+# for num in nums:
15
+# if num in freq_dict:
16
+# freq_dict[num] += 1
17
+# else:
18
+# freq_dict[num] = 1
19
+# sorted_list = sorted(freq_dict.keys(), key = lambda x: freq_dict[x], reverse=True)
20
+# return sorted_list[:k]
21
0 commit comments