O(N^2) solution in Python seems to be Timing Out


  • -1
    R

    The execution time seems to be 32-39ms yet this seems to be failing through the judge with TLE. Any suggestions?

    class Solution(object):
        def threeSum(self, nums):
            nums.sort();
            #print nums;
            result = [];
            for i in range(len(nums)-2):
                sum2 = -1*nums[i]
                left =i+1;
                right = len(nums)-1;
                #print "start",i,left,right
                while(left<right):
                    #print nums[left], nums[right], sum2
                    if (sum2<nums[left]+nums[right]):
                        right=right-1;
                    elif(sum2>nums[left]+nums[right]):
                        left=left+1;
                    else:
                      result.append([nums[i], nums[left], nums[right]]);
                      left=left+1;
                      right = right-1;
                    
            return list(set(result));
                                
    

  • 0

    Doesn't get TLE but Runtime Error.


Log in to reply
 

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