forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0347.py
36 lines (32 loc) · 951 Bytes
/
0347.py
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
# class Solution:
# def topKFrequent(self, nums, k):
# """
# :type nums: List[int]
# :type k: int
# :rtype: List[int]
# """
# from collections import Counter
# return [item[0] for item in Counter(nums).most_common(k)]
class Solution:
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
import heapq
count_list = dict()
for i in nums:
count_list[i] = count_list.get(i, 0) + 1
p = list()
result = list()
for i in count_list.items():
heapq.heappush(p, (-i[1], -i[0]))
if len(p) > len(count_list) - k:
_, val = heapq.heappop(p)
result.append(-val)
return result
if __name__ == '__main__':
nums = [4,1,1,1,2,2,3]
k = 2
print(Solution().topKFrequent(nums, k))