Max continuous subsequence and: Dynamic Programming Classical topics (2) __ max continuous subsequence and

Source: Internet
Author: User

Problem Description:

The maximum and the sequence of successive subsequence is actually the element and the largest one in the sequence of successive subsequence.

such as a given sequence:

{-2, 11,-4, 13,-5,-2}

The maximum contiguous subsequence is {11,-4, 13}, and the largest and 20.
===============================================================


Problem Analysis :

1. The first and simplest approach is violence O (n^3)

Direct two for loops enumerate the first and end of the subsequence, and then a loop computes the number of the sequence and the maximum value for each update.

But the complexity of this method is O (n^3), the efficiency is too low ...

————————————————————————————————————————————————

2. The second method is pretreatment O (n^2)

When you read it, you put the preceding number in the array, and you get an array sum[i] to store the number of the first I. Then, the sum of the subsequence is quickly obtained by using sums array.

In fact, this method only optimizes the previous method of calculation and the loop, the complex is O (n^2), also very bad.
————————————————————————————————————————————————

3. The third is the use of The thought of divide and conquer O (NLOGN)

Divide-and-conquer algorithm but look at the code is not very good understanding, in fact, the idea is very simple, that is, the sequence is divided into two of the calculation, using recursion to find out the largest sequence of two sequences, and then from the middle of the sequence to the two sides of the sequence to find the largest Returns the largest sequence and the.

The simplicity of the very magic, has been the problem of decomposition by a small problem to the next layer of recursion, until the bottom.

The complexity of the divide-and-conquer algorithm is better, O (Nlogn), although not the optimal solution, but understanding this algorithm can make us understand recursion more deeply.
————————————————————————————————————————————————

4. The fourth kind is the cumulative traversal algorithm O (n)

The sum is accumulated when the sequence is traversed, and if sum is less than 0 after it is accumulated, the sum is reset to negative infinity, and the maximum value of sum is updated each time. Finally, we can find the maximum value.

In fact, the algorithm is divided into several pieces of the sequence, each piece meet: for any k, the number of the first k and will not be less than 0 (less than 0 will be split into two blocks), the current I number and greater than the maximum when the update, and the maximum left edge is the first of the block sequence, the right side is the I.

Time complexity is O (n), and can be read side of the processing, do not need to open an array to save, space is also very province.

------------------------------------------------------------------------------

As an example:

-10 1 2 3 4-5 -23 3 7-21 (num)

-10 | 1 3 6 10 8 |  -23 | 3 10 | -21 (sum) (number of the sum is cleared 0)

Since 10 is the maximum of sum, the red block is the range of requirements.

------------------------------------------------------------------------------

But why the sequence is in the sequence block and is the first number of the sequence block to begin with it.

Certificate Ming : If this is not the case, there are several situations:

1 The target sequence spans several blocks. Since sum is reset to negative infinity when it is <=0, it's and certainly won't be the largest if it is not reset if it is spanned.

2 The target sequence is in the middle of the sequence block, and the start of the target sequence is not the first of the sequence. Because the number of the first k in the sequence block is not less than 0, so this certainly does not start from the first big.

————————————————————————————————————————————————

5. The fifth type is Dynamic Programming O (N)

DP practice is a common practice, as long as the state transfer equation can be worked out quickly.

State transition equation: sum[i] = max{sum[i-1]+a[i],a[i]}. (Sum[i] Records with A[i] as the maximum continuous and at the end of the subsequence. The maximum value of the sum array and two boundaries can be updated during the DP process.

In fact, you can not open the array, accumulated sum until sum + a < A, the sum assigned to a, update the maximum value on the line. You will find that this is the same as the 4th method ... Just judge the condition is not the same, one is sum <= 01 is sum + A < a ... (In fact, the same ...) So the complexity is the same as the fourth, and it's All O (n).


————————————————————————————————————————————————

6. The sixth type is Second Kind Cumulative Sum O (N)

(thank @shuangde800 for the guidance of the Great God)

This method, like the 4th, is a cumulative sum. We can see that the and of successive subsequence is actually (to the end of the and)-(to the first end-1 of the and), to make this equation the biggest actually makes it as small as possible (to the sum of the first end-1), we can traverse the array and maintain the minimum value (to the sum of the first end-1).

Understanding is the time to traverse, to find out the current position as the end of the previous point to the end of the and largest subsequence, and then update the maximum value. (In fact, this idea is similar to the fifth method)

The complexity is O (n) due to the need to traverse only once.

===============================================================

Pseudo Code:

because the following solution inside my code is too bad, so first put out the pseudo code.

1. Violence O (n^3):

[Plain]  View plain  copy  print? max← (-∞)    for i←1 to len do       for j←i  to len do           sum←0            //  sum             for k←i to j                sum←sum+arr[i]           end for           //  Update Max             if sum>max then                max←sum           end if       end for   end for   Return max  
2. Pretreatment O (n^2):

[plain] view plain copy print? Record before I and sum[0]←0 for i←1 to Len do

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.