LeetCode Top 100 Liked Questions 152. Maximum Product Subarray (Java version; Medium)

Keywords: Programming less Java

welcome to my blog

LeetCode Top 100 Liked Questions 152. Maximum Product Subarray (Java version; Medium)

Title Description

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:

Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Example 2:

Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

First time; dynamic programming; review the rules of sub-array dynamic programming; review the meaning of imax,imin: consider the maximum/minimum of the continuous sub-array of the current element; is a slightly concise version of the following code

class Solution {
    public int maxProduct(int[] nums) {
        //ensure a non-empty array
        int max = nums[0], imax = nums[0], imin = nums[0];
        for(int i=1; i<nums.length; i++){
            //Determine whether the current element is less than zero
            if(nums[i] < 0){
                int temp = imax;
                imax = imin;
                imin = temp;
            }
            //Update imax; maximum of continuous subarrays ending with nums[i]
            imax = Math.max(imax*nums[i], nums[i]);
            //Update imin; minimum value of continuous subarray ending with nums[i]
            imin = Math.min(imin*nums[i], nums[i]);
            //Update max
            max = Math.max(max, imax);
        }
        return max;
    }
}

First time; dynamic programming; Core 1: distinguish the difference between the maximum I max of continuous subarrays and the global maximum max; Global Max is the maximum of all cases so far, and IMAX is the maximum of current continuous subarrays (ending or beginning with nums [i]); Core 2: When nums [i] < 0, exchange the values of IMAX and imin; Because nums [i] < 0. ] <0, so I max*nums[i] becomes the minimum value of the current continuous subarray, imin*nums[i] becomes the maximum value of the current continuous subarray; core 3: initializing max,imax,imin uses nums[0]; core 4: dynamic programming of subarrays is not to select/not select the current state, but must consider the current state, select/not select the previous one. As a result, this is equivalent to a rule of subarray dynamic programming problems.

Stumbling case
int[] arr = {2,3,-2,4};

This example shows that not only imax but also imin need to be recorded, because negative is positive.
int[] arr2 = {-2,3,-4};

[-1,-2,-9,-6]
class Solution {
    public int maxProduct(int[] nums) {
        if(nums==null || nums.length==0)
            throw new RuntimeException();
        //Careful initialization
        int imin = nums[0], imax = nums[0], max = nums[0];
        for(int i=1; i<nums.length; i++){
            //If the current element is less than zero, the values of imax and imin need to be exchanged
            if(nums[i] < 0){
                int temp = imax;
                imax = imin;
                imin = temp;
            }
            //Conventional judgment; maximum and minimum values of continuous subarrays ending/beginning with nums[i]
            //Update imax; imax Meaning: Maximum of continuous subarrays ending / beginning with nums[i]
            if(imax*nums[i] > nums[i]){
                imax = imax*nums[i];
            }
            else{
                imax = nums[i];
            }
            //Update max
            max = Math.max(max, imax);
            //Update the meaning of imin; imin: the minimum of a continuous subarray ending or beginning with nums[i]
            if(imin*nums[i] < nums[i])
                imin = imin*nums[i];
            else
                imin = nums[i];
        }
        return max;
    }
}

Posted by ernielou on Wed, 02 Oct 2019 09:07:06 -0700