Python O(n) solution without sort, without heap, without quickselect


  • 3
    R
    class Solution(object):
        def topKFrequent(self, nums, k):
            hs = {}
            frq = {}
            for i in xrange(0, len(nums)):
                if nums[i] not in hs:
                    hs[nums[i]] = 1
                else:
                    hs[nums[i]] += 1
    
            for z,v in hs.iteritems():
                if v not in frq:
                    frq[v] = [z]
                else:
                    frq[v].append(z)
            
            arr = []
            
            for x in xrange(len(nums), 0, -1):
                if x in frq:
                    
                    for i in frq[x]:
                        arr.append(i)
    
            return [arr[x] for x in xrange(0, k)]
    

Log in to reply
 

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