【LeetCode】152. 乘积最大子数组

一、题目

给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

示例 1:

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

示例 2:

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

二、参考

1、递归

思路:

具体请结合代码及注释进行理解,运行超时,但可以训练自己对递归的理解。

代码:

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);
    // }

时间复杂度: O ( 2 n ) O(2^n) O(2n)
空间复杂度: O ( n ) O(n) O(n)

2、递归+记忆化

思路: 暂无。
代码: 暂无。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

3、动态规划

版本1

思路:

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]

代码:

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;
    }
}

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

版本2

思路:

由上可知,DP[i][0/1]只依赖于上一次运算结果DP[i-1][0/1],所以这里可以进行空间优化。具体代码如下:

代码:

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;
    }
}

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

三、参考

1、Possibly simplest solution with O(n) time complexity
2、乘积最大子数组
3、兄弟,我尽力了

猜你喜欢

转载自blog.csdn.net/HeavenDan/article/details/108982297