Python Easy DP Solution


  • 0
    K

    Keep Track of the total of the subarray. If the total is less than 0, then discard the previous part.

    class Solution(object):
    def maxSubArray(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    maxVal = -999999
    length = len(nums)
    if length == 0: return 0
    l, r, total = 0, 0, 0
    while r < length:
    total = total + nums[r]
    if maxVal < total:
    maxVal = total
    if total <= 0:
    r, l = r+1, r+1
    total = 0
    else:
    r = r+1
    return maxVal


Log in to reply
 

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