Easy python solution using dynamic programming beats 99.57% of the submissions


  • 0
    S
    class Solution(object):
        def maxSubArray(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            max_now = 0
            max_so_far = 0
            k = max(nums)
            if k<0:
                return k
            
            for i in range(len(nums)):
                max_now = max_now+nums[i]
                
                if max_now <0:
                    max_now = 0
                    
                elif max_now>max_so_far:
                    max_so_far = max_now
            return max_so_far
    
    

Log in to reply
 

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