my python solution is time exceeded


  • 0
    F

    '''
    class Solution(object):
    def containsDuplicate(self, nums):
    """
    :type nums: List[int]
    :rtype: bool
    """

        nums.sort()
        
        for i in nums:
            
            if len(nums) <=1:
                return False
            
            q = nums[0]
            nums = nums[1:]
            
            lower = 0
            upper = len(nums)-1
            
            while lower <= upper:
                mid = int((lower+upper)/2)
                if nums[mid] == q:
                    return True
                elif q < nums[mid]:
                    upper = mid - 1
                elif q > nums[mid]:
                    lower = mid + 1
                    
        return False
    

    '''

    I tried to implement binary search, and its still too slow to pass test case 17. is it possible to solve this in time without using 1 line python magic(i dont understand how those 1 liners work)


Log in to reply
 

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