python easy solution with time O(n) space O(1)


  • 1
    A

    start from left one can find we only need to check weather current sum is larger or smaller than zero. If smaller, we can re-start at right position.

    class Solution(object):
        def maxSubArray(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if not nums: return nums
            
            maxsum = nums[0]
            cursum = 0
            
            for i in range(len(nums)):
                
                cursum += nums[i]
                maxsum = max(maxsum,cursum)
                if cursum < 0:
                    cursum = 0
            
            return maxsum
            
    

Log in to reply
 

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