forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
347.py
33 lines (28 loc) · 749 Bytes
/
347.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
'''
Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2]
'''
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
if not nums:
return []
frequency = {}
for num in nums:
if num in frequency:
frequency[num] += 1
else:
frequency[num] = 1
result = []
import heapq
heap = []
for key, value in frequency.iteritems():
heapq.heappush(heap, (-value, key))
for _ in range(k):
result.append(heapq.heappop(heap)[1])
return result