My Solution keeps timing out but works on my computer, any suggestions?


  • 0
    S

    class Solution:

    def twoSum(self,l1,i):
    
        status = False
        for c in l1:
            j = l1.index(c) + 1
            while j < len(l1):
            
                total = c + l1[j]
            
                if total == i:
                    
                    status = True
                    return [l1.index(c)+1,j+1]
                    
            
                j += 1
            
            if status:
                break

  • 1
    Z

    You solution works but takes O(n^2) time. You may consider using hash table (dictionary) for O(n) algorithm.


Log in to reply
 

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