Leetcode 152. Maximum product Subarray (maximum product sub-array)

Source: Internet
Author: User

Find the contiguous subarray within an array (containing at least one number) which have the largest product.

For example, given the array [2,3,-2,4] ,
The contiguous Subarray has the [2,3] largest product = 6 .

Title Tags: Array, Dynamic programming

The topic gives us a nums array, which allows us to find a subarray whose product is the largest and returns the product value.

The difficulty of this problem is that there are 0 and a negative number, encountered 0, it is equal to the breakpoint, to restart the recording of a new section of the Subarray. If there is a negative number, if it is an even number of negative numbers, then you can still keep, if not, then start recording again. So this topic we need three variables, to constantly update the product of our subarray.

Traversing the nums array, max-records the maximum Subarray product from 0 to I.

Min-Record the smallest subarray product from 0 to I, here is the need to I, in front of any small segment I do not need, why to record the smallest, because there are negative, to the minimum negative values recorded, when encountering a new negative number, can be paired into an even number of negative numbers, the negative number is also used in the case.

Maxans-The value of the subarray that records the maximum product of any of the array.

Java Solution:

Runtime beats 42.46%

Completion Date: 08/28/2017

Keywords: Array, Dynamic programming

Key: Keep record the product value of the maximum and minimum subarray from 0 to I

1 classSolution2 {3      Public intMaxproduct (int[] nums)4     {5         if(Nums.length = = 0)6             return0;7         8         //Save first number into max, Min & Maxans9         intmax = Nums[0];Ten         intMin = nums[0]; One         intMaxans = Nums[0]; A          -         /*Iterate rest number - * For each number, remember the max and Min value for the previous product (0 ~ i) the         */ -          for(intI=1; i<nums.length; i++) -         { -             intTmp_max =Max; +             intTmp_min =min; -              +             //remember the max product Subarray from 0 to I Amax = Math.max (Math.max (nums[i], Tmp_max * nums[i]), Tmp_min *nums[i]); at             /*remember the min product Subarray from 0 to I - * min Product Subarray can only being from somewhere to I not somewhere to J, is before I - * Because each time max use min and if Min was not consecutive to current I, it was meaningless -             */ -min = Math.min (Math.min (nums[i], Tmp_max * nums[i]), Tmp_min *nums[i]); -              in             //Update the Maxans -Maxans =Math.max (max, Maxans); to         } +          -         returnMaxans; the     } *}

Resources:

Http://www.cnblogs.com/grandyang/p/4028713.html

Leetcode algorithm topic List- leetcode algorithms Questions list

Leetcode 152. Maximum product Subarray (maximum product sub-array)

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.