[LeetCode] 152. Maximum product sub-array

1. Title

Given an integer array nums, please find the continuous sub-array with the largest product in the array (the sub-array contains at least one number), and return the product corresponding to the sub-array.

Example 1:

输入: [2,3,-2,4]
输出: 6
解释: 子数组 [2,3] 有最大乘积 6

Example 2:

输入: [-2,0,-1]
输出: 0
解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。

Two, reference

1. Recursion

Ideas:

For details, please understand the code and comments. The running timeout, but you can train yourself to understand recursion.

Code:

class Solution {
    
    
    int max = Integer.MIN_VALUE;

    public int maxProduct(int[] nums) {
    
    
        if (nums == null || nums.length == 0) {
    
    
            return -1;
        }
        helper(nums, 1, 0);
        return max;
    }


    // 1-最大连续子列积
    private void helper(int[] nums, int product, int i) {
    
    
    	// Terminator
        if (i == nums.length) {
    
    
            return;
        }
		
		// Current logic:比较历史最大值与当前值,历史最大值形成增益则保留,否则丢弃
        int select = nums[i] * product;
        int max_value = Math.max(nums[i], select); // compare num[i] ? product*num[i]
        max = Math.max(max_value, max);

		// Drill down:保留有效数据,下一层可能会用到
        helper(nums, nums[i], i + 1);
        helper(nums, select, i + 1); 
    }
}

    // 2-最大不连续子列积
    // private void helper(int[] nums, int product, int i) {
    
    
    //     if (i == nums.length) {
    
    
    //         return;
    //     }
    //     int select = nums[i] * product;
    //     int max_value = Math.max(product, select);  // 差异
    //     max = Math.max(max_value, max);
    //     helper(nums, product, i + 1);  // 重点差异
    //     helper(nums, select, i + 1);
    // }

Time complexity: O (2 n) O(2^n)O ( 2n )
Space complexity: O (n) O(n)O ( n )

2. Recursion + memoization

Ideas: No.
Code: None.
Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )

3. Dynamic planning

version 1

Ideas:

1、状态定义:DP[i][2]
DP[i][0]:最大值
DP[i][1]:最小值

2、转移方程:DP[i]=DP[i-1]*a[i]
DP[i][0] = a[i]>=0 ? DP[i-1][0]*a[i] : DP[i-1][1]*a[i]
DP[i][1] = a[i]>=0 ? DP[i-1][1]*a[i] : DP[i-1][0]*a[i]

return DP[i][0]

Code:

class Solution {
    
    
    public int maxProduct(int[] nums) {
    
    
        if (nums==null || nums.length==0) return 0;
        int[][] dp = new int[nums.length][2];
        int res=nums[0]; dp[0][0]=nums[0]; dp[0][1]=nums[0];
        for (int i=1; i<nums.length; i++) {
    
    
            dp[i][0] = Math.max( Math.max(dp[i-1][0]*nums[i], dp[i-1][1]*nums[i]), nums[i]);
            dp[i][1] = Math.min( Math.min(dp[i-1][0]*nums[i], dp[i-1][1]*nums[i]), nums[i]);
            res = Math.max(res, dp[i][0]);
        }
        return res;
    }
}

Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )

Version 2

Ideas:

It can be seen from the above that DP[i][0/1] only depends on the last operation result DP[i-1][0/1], so space optimization can be performed here. The specific code is as follows:

Code:

class Solution {
    
    
    public int maxProduct(int[] nums) {
    
    
        if (nums==null || nums.length==0) return 0;
        int currMax = nums[0], currMin = nums[0], ans = nums[0];
        for (int i = 1; i < nums.length; ++i) {
    
    
            int mx = currMax, mn = currMin;
            currMax = Math.max(mx * nums[i], Math.max(nums[i], mn * nums[i]));
            currMin = Math.min(mn * nums[i], Math.min(nums[i], mx * nums[i]));
            ans = Math.max(currMax, ans);
        }
        return ans;
    }
}

Time complexity: O (n) O(n)O ( n )
space complexity: O (1) O(1)O ( 1 )

Three, reference

1. Possibly simplest solution with O(n) time complexity
2. Maximum product sub-array
3. Brother, I tried my best

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/108982297