LeetCode 53. The maximum and subsequence (java)

Dynamic programming class topic, defines two variables a and Maxx, for recording a maximum value of the sum of each number and before, both dp [i], if dp [i-1]> 0, then the DP [ i] + dp [i-1] must be greater than dp [i], is smaller than the other hand, it is only necessary every time a determination is greater than 0, updating the value of a, and determines a size and maxx, maxx update.

 

class Solution {
    public int maxSubArray(int[] nums) {
        int maxx=nums[0];
        int a=nums[0];
        for(int i=1;i<nums.length;i++)
        {
            if(a>=0)
                a=nums[i]+a;
            else
                a=nums[i];
            if(a>maxx)
                maxx=a;
        }
        return maxx;
    }
    
}

 

Guess you like

Origin www.cnblogs.com/y1040511302/p/11285890.html