Python solution O(n)


  • 0
    D

    class Solution(object):
    def maxSubArray(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    maxSum=nums[0]
    numSum=nums[0]

        for i in range(len(nums)-1):
            if numSum>0:
                numSum+=nums[i+1]
            else:
                numSum=nums[i+1]
                
            maxSum=max(maxSum,numSum) 
            
        return maxSum

Log in to reply
 

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