Programming method: Interview and algorithm experience (maximum continuous product substring)

Source: Internet
Author: User

Tag: Represents a For Loop string Commons products Xen use order BST

all the content comes from the programming method: Interview and algorithm experience book, the implementation is written by themselves using JavaTitle Description

Given a sequence of floating-point numbers, taking the value of the maximal product continuous substring, such as -2.5,4,0,3,0.5,8,-1, the maximum product continuous substring fetched is 3,0.5,8. That is, in the above array, 3 0.5 8 The product 30.58=12 of the 3 numbers is the largest, and is continuous.

Analysis and Solution

This maximal product continuous substring is different from the maximal product subsequence, do not confuse, the former substring requires continuous, the latter sub-sequence does not require continuous. In other words, the longest common substring (longest commonsubstring) and the longest common subsequence (Longestcommon Subsequence,lcs) are:

    • A substring (Substring) is a continuous part of a string,
    • The subsequence (subsequence) is a new sequence obtained from removing arbitrary elements from the sequence without altering the sequence;

To be more brief, the former (substring) must have a continuous character position, while the latter (the subsequence LCS) does not have to be. For example, the longest common substring of the string "ACDFG" and "AKDFC" is "DF", and their longest common subsequence LCS is "ADF", and LCS can be solved by using dynamic programming.

Solution One

Perhaps the reader may immediately think of the simplest and most brutal way: Two for Loop direct polling.

But the time complexity of this brute force method is O (n^2), can you think of ways to reduce the complexity of time?

Solution Two

Given that there is a positive negative in the product sequence, there may be 0, we can simplify the problem to this: the array to find a sub-sequence, so that it is the largest product, and find a sub-sequence, so that its product is the smallest (negative case). Because although we only need a maximum product, but because of the existence of negative numbers, we also find the two products to do it instead of convenient. In other words, not only the maximum product is recorded, but also the minimum product is recorded.

Assuming that the array is a[], the direct use of dynamic programming to solve, considering the possibility of negative numbers, we use Maxend to represent the largest continuous substring ending with a[i], the product value of the smallest substring ending with the minend, the state transition equation is:

  * a[i], minend * a[i]), a[i]);  minend = min(min(maxend * a[i], minend * a[i]), a[i]);

The initial state is maxend = Minend = A[0].

The reference code is as follows:

  Public Static DoubleMaxproductsubstring (Double[] a) {DoubleMaxend = a[0]; DoubleMinend = a[0]; DoubleMaxresult = a[0];  for(inti = 1; i < a.length; ++i) {DoubleEnd1 = Maxend * A[i], End2 = Minend *A[i]; Maxend=Math.max (Math.max (End1, End2), A[i]); Minend=math.min (Math.min (End1, End2), A[i]); Maxresult=Math.max (Maxresult, maxend); }        returnMaxresult; }

Dynamic programming Solution A For loop is done, so the time complexity is O (n).

Programming method: Interview and algorithm experience (maximum continuous product substring)

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.