Why is my solution so elegant yet slow?


  • 0
    U

    class Solution(object):
    def topKFrequent(self, nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: List[int]
    """

        if len(nums) <=1:
            return nums
        
        max_list = sorted(set(nums), key=lambda x:nums.count(x), reverse=True)
        
        return max_list[:k]
    

    I have no idea why this is super slow. Any explaination would be super helpful! Thanks. :)


Log in to reply
 

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