LeeCode53 maximum subsequence sum (Java) (dp dynamic programming)

Topic link: LeeCode53 maximum sub-sequence and
topic description: Insert picture description here
simple dp, when recording to the current position, judge the previously recorded maximum sub-sequence sum and the maximum sub-sequence sum added to the current position, the maximum value is the maximum sub-sequence sum
Insert picture description here

class Solution {
    
    
    public static int maxSubArray(int[] nums) {
    
    
        int[] dp=new int[nums.length];
        int max=nums[0];
        dp[0]=nums[0];
        for (int i = 1; i < nums.length; i++) {
    
    
            dp[i]= Math.max(nums[i],nums[i]+dp[i-1] );
            max= Math.max(max,dp[i]);
        }
        return max;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43590593/article/details/112604327