Can anyone tell me why I got different runtime of same code?


  • 0
    F

    This code can sometimes beat 99.4% of python submission which cost about 40ms.
    However, sometimes, it cost 52ms. Could anyone please tell me the reason behind?

    class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        my_collection = collections.defaultdict(lambda :0)
        i=0
        for num in nums:
            my_collection[num] = i
            i = i+1
        i=0
        for number in nums:
            if ((target-number) in my_collection) and my_collection[target-number] != i:
                return [i,my_collection[target-number]]
            i=i+1

  • 0
    D

    Just because the inputs are different take it easy


  • 0
    F

    Hi Debaucher,

    Thanks for your answer. So how can I tell how good or bad my code is if they use different dataset as input?

    Cheers


Log in to reply
 

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