Skip to content

Commit e07436c

Browse files
committed
Top K Frequent Elements
1 parent 8918c97 commit e07436c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

top-k-frequent-elements/thispath98.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
# Time Complexity: O(N log N)
3+
- Counter 생성: N번 순회
4+
- most common 연산: N log N
5+
# Space Compelexity: O(N)
6+
- 최악의 경우 (중복된 값이 없을 경우) N개 저장
7+
"""
8+
from collections import Counter
9+
10+
11+
class Solution:
12+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
13+
count_dict = Counter(nums)
14+
top_k_list = count_dict.most_common(k)
15+
answer = [key for key, value in top_k_list]
16+
return answer

0 commit comments

Comments
 (0)