Leetcode Maximum Product subarray solution report

Source: Internet
Author: User
The new leetcode question is updated, and the maximum sub-array Product
Https://oj.leetcode.com/problems/maximum-product-subarray/

Question Analysis: calculates the maximum product of an array and consecutive sub-arrays.
Solution: The simplest way of thinking is to repeat three times, calculate the value of productarray [I] [J] (productarray [I] [J] is the product of a [I] to a [J]), and record the maximum value, algorithm time complexity O (N3), must be TLE.

The first optimization, dynamic planning, and solution: when using productarray [I] [J], we do not need to loop from I to J again, but use: productarray [I] [J] = productarray [I] [J-1] * A [J]; the time complexity of the algorithm is O (n2 ), unfortunately, it's also 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;    }}

The second optimization, in fact, the possibility of the Maximum Product of the sub-array is: the maximum value of the tired multiplication encounters a positive number; or, the minimum value of the tired multiplication (negative) encounters a negative number. Therefore, the maximum (positive) and minimum (negative) values of the multiplication are saved each time ). At the same time, there is a logic to select the starting point. If the current element is not large (or small) after the maximum and minimum values are multiplied by the current element, the current element can be used as a new starting point. For example, if the previous element is 0, when {, 9, 2}, to 9, 9 should be used as a maximum value, that is, a new starting point, {,-9, -2} is the same.-9 is smaller than the current minimum value, so it is updated to the current minimum value.
This method only needs to traverse the array once, and the algorithm time complexity is 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;    }}









Leetcode Maximum Product subarray solution report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.