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

  • 0

    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:

  • 1

    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.