LeeCode152 product maximum sub-array (Java) (dp dynamic programming)

Topic link: LeeCode152 product maximum sub-array
Topic description: Insert picture description here
The difference from the maximum substring sum is that there is a negative number and the maximum number will become the smallest number after the negative number is multiplied, so the largest solution may be the smallest number times the current number or the largest number times the current number Number or current number itself

class Solution {
    
    
    public static int maxProduct(int[] nums) {
    
    
        int max=Integer.MIN_VALUE,maxx=1,minn=1,premax=1;
        for (int i = 0; i < nums.length; i++) {
    
    
            premax=maxx;
            maxx=Math.max(Math.max(minn*nums[i],maxx*nums[i]),nums[i]);
            minn=Math.min(Math.min(minn*nums[i],premax*nums[i]),nums[i]);
            max=Math.max(max,maxx);
        }
        return max;
    }
}

Guess you like

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