Why my python solution Time Limit Exceeded ?


  • 0
    L
        lenN = len(nums)
        if lenN < 2:return nums
        result_1 =[]
        def product(nums, k):
            lenN = len(nums)
            temp = 1
            for i in range(0, lenN):
                if i == k:
                    continue
                temp *= nums[i]
            return temp
        for i in range(0,lenN):
            result_1.append(product(nums,i))
        return result_1

  • 2
    K

    It's clearly an O(n^2) algorithm. The problem requires that your code runs in O(n) time.


Log in to reply
 

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