LeetCode Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积

https://oj.leetcode.com/problems/maximum-product-subarray/

题目分析:求一个数组,连续子数组的最大乘积。

解题思路:最简单的思路就是3重循环,求解productArray[i][j]的值(productArray[i][j]为A[i]到A[j]的乘积),然后记录其中最大值,算法时间复杂度O(n3),必然TLE。

第一次优化,动态规划,求解:productArray[i][j]的时候不用再次循环从i到j,而是利用:productArray[i][j]=productArray[i][j-1]*A[j];采用递推的方法来计算,算法时间复杂度为O(n2),遗憾的是也TLE了。

public class Solution {
    public int maxProduct(int A[]) {
        if(A==null||A.length==0) {
	      return 0;
	    }
	    int[][] productArray =  new int[A.length][A.length];
	    int maxProduct = A[0];

	    for(int i=0;i<A.length;i++) {
	    	for(int j=i;j<A.length;j++) {
	    		if(j==i) {
	    		    productArray[i][i] = A[i];
	    		} else {
	    		    productArray[i][j] = productArray[i][j-1]*A[j];
	    		}
	    		if(productArray[i][j]>maxProduct) {
	    			maxProduct = productArray[i][i];
	    		}
	    	}
	    }
	    return maxProduct;
    }
}

第二次优化,其实子数组乘积最大值的可能性为:累乘的最大值碰到了一个正数;或者,累乘的最小值(负数),碰到了一个负数。所以每次要保存累乘的最大(正数)和最小值(负数)。同时还有一个选择起点的逻辑,如果之前的最大和最小值同当前元素相乘之后,没有当前元素大(或小)那么当前元素就可作为新的起点。例如,前一个元素为0的情况,{1,0,9,2},到9的时候9应该作为一个最大值,也就是新的起点,{1,0,-9,-2}也是同样道理,-9比当前最小值还小,所以更新为当前最小值。

这种方法只需要遍历一次数组即可,算法时间复杂度为O(n)。

public class Solution {
    public int maxProduct(int A[]) {
        if(A==null||A.length==0) {
	      return 0;
	    }
	    int maxProduct = A[0];
	    int max_temp   = A[0];
	    int min_temp   = A[0];

	    for(int i=1;i<A.length;i++) {
	    	int a = A[i]*max_temp;
	    	int b = A[i]*min_temp;
	    	max_temp = Math.max(Math.max(a,b), A[i]);
	    	min_temp = Math.min(Math.min(a,b), A[i]);
	    	maxProduct = Math.max(maxProduct, max_temp);
	    }
	    return maxProduct;
    }
}

时间: 2024-07-28 13:21:12

LeetCode Maximum Product Subarray 解题报告的相关文章

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode: Maximum Product Subarray &amp;&amp; Maximum Subarray

Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 对于Product

152. Maximum Product Subarray 解题记录

题目描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 这道题可分为两种情况: 1) 有非0的元素,首先分析当数组

[LeetCode] Maximum Product Subarray的两种思路

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这题和之前的一题Maximum Subarray非常类似,一个是求最大和,而这个是

[leetcode] Maximum Product Subarray @ python

[leetcode] Latest added: 2014-09-23 Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Trick:

LeetCode——Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 原题链接:https://oj.leetcode.com/problems/max

[LeetCode]Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的. 如

[LeetCode] Maximum Product Subarray 连续数列最大积

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Hide Tags Array Dynamic Programming 这个问题是给

DP Leetcode - Maximum Product Subarray

最近一直忙着写paper,很久没做题,一下子把题目搞复杂了..思路理清楚了非常简单,每次只需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]>0,乘以前面最大的数(>0),得到新的最大乘积:当前A[i]<0,乘以前面最小的数(<0),得到新的最大乘积:A[i]它自己>0,(A[i-1]==0.最小乘积同理.. class Solution { public: int Max(int a, int b, int c) { int ma