-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtop-k-frequent-elements.py
More file actions
54 lines (40 loc) · 1.59 KB
/
top-k-frequent-elements.py
File metadata and controls
54 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import random
from collections import Counter
from typing import List
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
arr = list(map(lambda x: (x[1], x[0]), Counter(nums).items()))
def quicksort_up_to_k(left, right):
if left >= right:
return
pivot = random.randint(left, right)
arr[pivot], arr[right] = arr[right], arr[pivot]
sorted_ptr = left
for pos in range(left, right + 1):
if arr[pos] >= arr[right]:
arr[sorted_ptr], arr[pos] = arr[pos], arr[sorted_ptr]
sorted_ptr += 1
if sorted_ptr == k:
return
elif sorted_ptr < k:
quicksort_up_to_k(sorted_ptr, right)
elif sorted_ptr > k:
quicksort_up_to_k(left, sorted_ptr - 1)
quicksort_up_to_k(0, len(arr) - 1)
return [arr[pos][1] for pos in range(k)]
def topKFrequentStraightforward(self, nums: List[int], k: int) -> List[int]:
return list(
map(
lambda x: x[0],
sorted(((k, v) for k, v in Counter(nums).items()), key=lambda x: x[1]),
)
)[-k:]
class TestSolution:
def setup(self):
self.sol = Solution()
def test_case1(self):
assert self.sol.topKFrequent([1, 1, 1, 2, 2, 3], 2) == [1, 2]
def test_case2(self):
assert self.sol.topKFrequent([3, 0, 1, 0], 1) == [0]
def test_case3(self):
assert self.sol.topKFrequent([5, 3, 1, 1, 1, 3, 5, 73, 1], 3) == [1, 5, 3]