Python solution : Using Heap

  • 0
    class Solution(object):
        def topKFrequent(self, nums, k):
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            d = {}
            h = []
            ans = []
            for i in nums:
                d[i] = d.get(i,0) + 1
            for num, value in d.iteritems():
            for i in heapq.nlargest(k, h, key = lambda e:e[1]):
            return ans

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.