Maximal continuous sub-array algorithm learning

Source: Internet
Author: User

As a 0 basic learning of the mentally handicapped after a difficult career, in the dark after a few months in the struggle, only to think of a blog should be open to record their own learning process and finishing knowledge points. I have just contacted the algorithm, the blog to record my algorithm learning process as the beginning of it. More useless, the following begins:

If the stock is up and down after 30 days, how can I determine the maximum value of my earnings? Here can be stored in a daily change of the stock in an array, up to remember as positive, down as negative, then the actual problem is converted to the maximum continuous sub-array problem, that is, how do I cut this array so that the value of the array is the largest? Here is a simple division of the method to calculate, first the data is divided into 2 parts, one for the left data, one for the right data, then you can use the loop to find the respective maximum of the data, the left array (below the Python code)

    # Left Array    MID = Len (data)/2    = 0    = 0    = Data[:mid]    = 0      for  in range (len (leftdata) -1,-1,-1):        = ssum + leftdata[i]        if ssum > sumleft:            = ssum            = i

Mid is the midpoint of data, the data is divided into 2 parts with mid, the left array is leftdata, and the accumulated value in the left array is computed by ssum, and the maximum value of leftdata is accumulated. If the accumulated value is greater than the maximum value, then the accumulated value is stored as the maximum value. Right array

    # Right array    Sumright = 0    = 0    = data[mid:]    = 0    for in  Range (len (rightdata)):        = ssum +rightdata[j]        if ssum >  Sumright:            = ssum            = j

This will be able to calculate the left and right 2 sides of the largest continuous sub-array, is not compared with each other after returning the larger one OK? No, the original ignored one, that is, if the largest sub-array across mid-midpoint it? We also have to compare the largest subarray of this array with the following:

    Crossdata = Data[leftindex:rightindex + mid + 1]    = 0    = 0    = 0       for in range (len (crossdata)):        = ssum + crossdata[x]        if Ssum > sumcross:            = ssum            = x

Before using I,J to record the maximum index of the left array I, and the maximum index of the right array J, then you can think of this cross array must be in the i,j these 2 boundaries, because these 2 are already 2 sides of the maximum value, if added beyond the boundary portion of the value must be less than the maximum value.

So the data can be cut to crossdata, here to note that J because it is the right array, so j in the entire data should also need to add the midpoint of the value to really get to the right edge of the crossdata, because the cutting time does not include the last J, in order to put the right boundary in the calculation, but also must add 1

Then, according to the previous routines, find out the maximum value of Crossdata Sumcross

Well, the next 3 values are compared to return the largest one.

Maxsum = max (Sumright,sumleft,sumcross)

As a result of the new study, the algorithm has a lot of things to improve, can only be step by step.

Maximal continuous sub-array algorithm learning

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.