what's wrong with my code??


  • 0
    I

    time limit exceeded
    '''
    class Solution(object):
    def maxSubArray(self,nums):

        largest = []
    
        if len(nums) == 1:
            return nums[0]
    
        if len(nums) <1001:
    
            for n in range(len(nums)):    
                subArr = []
                i = n
    
                while i < len(nums):
                    i2 = 0
                    if nums[i] <=0:
                        stock = []
                        for i2 in range(i+1,len(nums)):
                            stock.append(nums[i2])
                            if nums[i] + sum(stock)>0:
                                subArr.append(nums[i])
                                for n in range(len(stock)):
                                    subArr.append(stock[n])
                                i = i2
                                break
                    else:
                        subArr.append(nums[i])               
    
                    if not subArr:
                        subArr.append(nums[i])  
    
                    if i2 == len(nums)-1:
                        i = i2                  
    
                    i +=1
    
                if not largest:
                    largest = subArr
                else:
                    if sum(largest) < sum(subArr):
                        largest = subArr
    
            return sum(largest)              
    

    '''


  • 0
    V

    @ImTheOne It seems really confusing. I am not sure what your algorithm is but I see three for loops and I imagine they have a time restriction to O(n^2) for the problem. Thus your solution fails the bounds set. :(

    Try using a known algorithm/strategy like DP or Divide and Conquer which have time complexities of O(n) and O(n lg n) respectively.


Log in to reply
 

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