Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d24a6e5

Browse filesBrowse files
committed
Time: 201 ms (12.90%), Space: 18.8 MB (40.43%) - LeetHub
1 parent 6b86ee8 commit d24a6e5
Copy full SHA for d24a6e5

File tree

1 file changed

+11
-0
lines changed
Filter options

1 file changed

+11
-0
lines changed
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import heapq
2+
from collections import Counter
3+
class Solution:
4+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5+
res = []
6+
dic = Counter(nums)
7+
max_heap = [(-val, key) for key, val in dic.items()]
8+
heapq.heapify(max_heap)
9+
for i in range(k):
10+
res.append(heapq.heappop(max_heap)[1])
11+
return res

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.