Maximum stock return problem (array maximum difference)

Source: Internet
Author: User
Tags stock prices
The maximum stock return problem (array maximum difference problem) problem description

Given an array that stores stock prices sorted by time, the element of position I is the stock price at the time of the first transaction; Suppose you only allow one buy at a time and then sell it at a certain moment, design an algorithm that solves the maximum benefit you may have, if the share price is not increased, the gain is 0. O (N2) solution

Compare each element of the array with all subsequent elements, choosing the one that is the biggest difference.
The complexity of the time is:
T (n) = (n−1) + (n−2) +......+1

O (NLOGN) solution algorithm idea

Using the idea of divide and conquer, divide the array in half, the maximum proceeds are divided into the following 3 cases:
-Buy and sell in the first half of the paragraph
-Buy and sell in the latter half of the paragraph
-Buy in the first half, sell in the latter half of the paragraph
For both the 1th and 2nd cases, just see it as a new problem of scale reduction, that is, iterative our algorithm, for the 3rd case, need to find the smallest element min in the first half, in the second half of the maximum element max, then max−min is the 3rd of the possible optimal solution. Pseudo code

void stock (int k[], int startIndex, int endindex) {
    int length = Endindex-startindex;
    if (1==length) return 0;
    if (2==length) return K[endindex]-k[startindex] > 0? K[endindex]-k[startindex]: 0;
    if (length>2) {
        Midindex = (startindex+endindex)/2;
        min = Findmin (K, StartIndex, midindex);
        max = Findmax (K, midindex+1, endindex);
        Mayget = max (stock (K,startindex,midindex), Stock (K,startindex,midindex), max-min);
        Return mayget > 0? mayget:0
    }
}
Degree of complexity

T (n) =2∗t (n2) +n2+n2
To solve:
T (n) =o (NLOGN)

The idea of O (n) Solution algorithm

The preprocessing array is transformed into the maximal segment and the problem of the array, and then the maximal sub segment and the solution algorithm are used to solve the problem. Steps: Preprocessing arrays

Subtracts each item of an array from the previous item next to it, with the first item set to 0;
The meaning is: the array of stock prices into an array of price increases.
Such as:

K = [3,5,1,2,5,8,9,6]

Processed and then changed to

K = [0,2,-4,1,3,3,1,-3]

After preprocessing, the original problem is transformed into the problem of finding the maximal sub segment of the array.
For example, in the original array it is obvious that the maximum gain is 9−1=8, then in the preprocessed array, only the first element after 1 should start to the corresponding element of 9, and then add to the 1+3+3+1=8.
This is also very simple, because after the preprocessing of the array storage is exactly increment, the two elements between all the increments, get exactly two elements of the difference.
Preprocessing algorithm pseudo code:

void preprocess (int k[], int n) {
    int i;
    i = n-1;
    while (i>0) {
        k[i] = k[i]-k[i-1];
    }
    K[0] = 0;
}
Find the largest segment and

Then the problem of maximal benefit is transformed into the problem of finding the maximal sub segment of the array, and the following algorithm is used for the maximal sub segment.
First Look at pseudocode:

void maxsegsum (int k[], int n) {
    int i;
    int maxsum=-maxint;
    int tempsum=0;
    For (i=0 i<n; i++) {
        TempSum + = K[i];
        if (tempsum>maxsum) maxsum = tempsum;
        if (tempsum<0) tempsum=0;
    }   
    Print ("Max sub-segment sum is%f", maxsum);
}

Algorithm Description:
-Iterate through the array, maintaining two global variables, maxsum record the optimal solution, TempSum record the segment and the beginning of a position;
-After each update tempsum, compare with maxsum, update maxsum more than Maxsum, and then check if the tempsum is less than 0, if it continues to accumulate, if it is tempsum to zero;
-After iterating through the array, the resulting maxsum is the largest segment of the array and. The algorithm summarizes the preprocessing array, converts to the maximal sub segment and question O (n), and solves the maximum sub segment and problem O (n) using the above algorithm. Complexity of Time

T (n) =o (n) +o (n)
That
T (n) =o (n)

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.