Skip to content

Commit efa70e7

Browse files
committed
3 solved
1 parent fe14d8f commit efa70e7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Blind75 - top-k-frequent-elements
3+
https://leetcode.com/problems/top-k-frequent-elements/
4+
"""
5+
6+
from typing import List
7+
from collections import Counter
8+
9+
class Solution:
10+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
11+
# (1,1,2,2,2,3) -> {1:1, 2:3, 3:1}
12+
num_counter = Counter(nums)
13+
# {1:1, 2:3, 3:1} -> [(2,3), (1,1), (3,1)]
14+
sorted_items = sorted(num_counter.items(), key=lambda x: x[1], reverse=True)
15+
return [item[0] for item in sorted_items[:k]]

0 commit comments

Comments
 (0)