Skip to content

Commit 2771cad

Browse files
committed
feat: Solve top-k-frequent-elements problem
1 parent 521c32a commit 2771cad

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

top-k-frequent-elements/hu6r1s.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import Counter
2+
3+
class Solution:
4+
"""
5+
- TC
6+
- Counter(nums): O(n)
7+
- sorted(): O(n log n)
8+
- 슬라이싱 및 리스트 변환: O(k)
9+
- 전체: O(n log n)
10+
- SP
11+
- Counter 및 정렬된 딕셔너리 저장: O(n)
12+
- 반환 리스트: O(k)
13+
- 전체: O(n)
14+
"""
15+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
16+
count_nums = Counter(nums)
17+
count_nums = dict(sorted(count_nums.items(), reverse=True, key=lambda x: x[1])).keys()
18+
return list(count_nums)[:k]

0 commit comments

Comments
 (0)