Skip to content

Commit 2716a3f

Browse files
committed
Update top-k-frequent-elements.py
1 parent 35f7f0a commit 2716a3f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Python/top-k-frequent-elements.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ def PartitionAroundPivot(left, right, pivot_idx, nums):
6161
else: # new_pivot_idx < k - 1.
6262
left = new_pivot_idx + 1
6363

64+
65+
# Time: O(nlogk)
66+
# Space: O(n)
67+
class Solution2(object):
68+
def topKFrequent(self, nums, k):
69+
"""
70+
:type nums: List[int]
71+
:type k: int
72+
:rtype: List[int]
73+
"""
74+
return [key for key, _ in collections.Counter(nums).most_common(k)]
75+

0 commit comments

Comments
 (0)