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)              
    

    '''


Log in to reply
 

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