Easy to understand DP Python O(n) solution


  • 0
    A
    class Solution(object):
        def maxSubArray(self, nums):
          
            n=len(nums)
            maxsofar=max(nums)
            temp=0
            for i in range(n):
                temp+=nums[i]
                if temp>=0:
                    maxsofar=max(maxsofar, temp)
                else: temp=0
                
            return maxsofar

Log in to reply
 

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