Python, Time Limit Exceeded


  • 0
    K

    It says run time limit exceeded. I'm new to python and coding. Does my code have run time n~2?

    '''
    class Solution(object):
    def totalHammingDistance(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    total=0
    for i in range (0,len(nums)-1):
    for j in range (i,len(nums)-1):
    total=total+bin(nums[i]^nums[j+1]).count("1")
    return total
    '''


  • 0
    N

    n^2 run time indeed...if you consider the time spent on count function, it would be more than that


  • 0
    K

    Thank you! So does the Time Limit Exceeded message mean it's not working code? Or it means it's just bad implementation that using too much resource so fail the grading. Sorry I'm new to coding, still don't have a clue.


  • 0
    N

    @kittensing The time limit is put to block algorithms that are not good enough.Generally, your code may exceed the limit if the standard method is way better than yours or there is an endless loop in code. In this case, your implementation is fine, but the method needs to be improved :) I am newbee too.


Log in to reply
 

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