Python super easy solution


  • 0
    E
    class Solution(object):
        def topKFrequent(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: List[int]
            """
            s = set(nums)
            l = []
            rt = []
            for i in s:
                l.append((nums.count(i), i))
            l.sort()
            while k > 0:
                m = l.pop()
                rt.append(m[1])
                k-=1
            return rt
            
    

  • 1
    A

    The question says you must do it better than O(n logn). You should also take into account that nums.count(i) is an O(n) operation.


Log in to reply
 

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