Leetcode: Maximum Product subarray

Source: Internet
Author: User

Question: 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. 

This question is a type of dynamic planning. Previously, maximum subarray was commonly used. Now it is product subarray, but the idea is consistent.
Of course, there is no need for dynamic planning. The conventional method can also be used, but the time complexity is too high (timeout), as shown below:

1 // train of thought: Use two pointers to point to the beginning and end of the word group 2 int maxproduct (int A [], int N) 3 {4 assert (n> 0 ); 5 Int subarrayproduct =-32768; 6 7 for (INT I = 0; I! = N; ++ I) {8 int ntempproduct = 1; 9 for (Int J = I; J! = N; ++ J) {10 if (j = I) 11 ntempproduct = A [I]; 12 else13 ntempproduct * = A [J]; 14 if (ntempproduct> = subarrayproduct) 15 subarrayproduct = ntempproduct; 16} 17} 18 return subarrayproduct; 19}

The dynamic planning method is to find its transfer equation, also called the recursive formula of dynamic planning. The dynamic planning solution is nothing more than maintaining two variables, the local optimum and the global optimum, let's take a look at the situation of maximum subarray. If there is a negative number, the value after addition must be smaller than the original value, but it may be larger or smaller than the current value. Therefore, for addition, you only need to be able to process the relationship between the local maximum and the global maximum. For this, write the transfer equation as follows:
Local [I + 1] = max (local [I] + A [I], a [I]);

Global [I + 1] = max (local [I + 1], global [I]);

The Code is as follows:

 1 int maxSubArray(int A[], int n) 2 { 3     assert(n > 0); 4     if (n <= 0) 5         return 0; 6     int global = A[0]; 7     int local = A[0]; 8      9     for(int i = 1; i != n; ++ i) {10         local = MAX(A[i], local + A[i]);11         global = MAX(local, global);12     }13     return global;14 }

For product subarray, we need to consider a special situation, that is, multiplying a negative number and a negative number: if a small negative number is obtained before, it is multiplied by a large negative number, the result is a large number, such as {2,-3,-7}. Therefore, when processing multiplication, we need to maintain a local maximum value, at the same time, a local minimum value must be maintained. Therefore, the following transfer equation can be written:

Max_copy [I] = max_local [I]
Max_local [I + 1] = max (max_local [I] * A [I], a [I]), min_local * A [I])

Min_local [I + 1] = min (max_copy [I] * A [I], a [I]), min_local * A [I])

The Code is as follows:

 1 #define MAX(x,y) ((x)>(y)?(x):(y)) 2 #define MIN(x,y) ((x)<(y)?(x):(y)) 3  4 int maxProduct1(int A[], int n) 5 { 6     assert(n > 0); 7     if (n <= 0) 8         return 0; 9 10     if (n == 1)11         return A[0];12     int max_local = A[0];13     int min_local = A[0];14 15     int global = A[0];16     for (int i = 1; i != n; ++ i) {17         int max_copy = max_local;18         max_local = MAX(MAX(A[i] * max_local, A[i]), A[i] * min_local);19         min_local = MIN(MIN(A[i] * max_copy, A[i]), A[i] * min_local);20         global = MAX(global, max_local);21     }22     return global;23 }

Summary: The Core step of dynamic planning is to write the state transition equation, but how to write the correct equation requires constant practice and summarization.

Leetcode: Maximum Product subarray

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.