Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions top-k-frequent-elements/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
from collections import defaultdict
frequent_dict = defaultdict(int)
for num in nums:
frequent_dict[num] += 1
sorted_dict = dict(sorted(frequent_dict.items(), key=operator.itemgetter(1), reverse=True))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator 모듈을 사용하고 있지만 import되지 않았습니다.

return list(sorted_dict)[:k]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list(dict)는 key만 리스트로 변환되므로, 여기서는 top k frequent elements를 의미하긴 하지만 비효율적입니다.