Skip to content

Commit 119514c

Browse files
committed
237
1 parent c527927 commit 119514c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

top-k-frequent-elements/jeldo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from collections import Counter
2+
import heapq
3+
4+
5+
class Solution:
6+
# O(nlogn)
7+
def topKFrequent(self, nums: list[int], k: int) -> list[int]:
8+
ls = [(key, value) for key, value in Counter(nums).items()] # O(n)
9+
return [key for _, key in heapq.nlargest(n=k, iterable=ls)] # O(nlogn)

0 commit comments

Comments
 (0)