Leetcode--best time to Buy and Sell Stock III

Source: Internet
Author: User

Problem Description:

Say you has an array for which the i-th element is the price of a given-stock on day I.

Design an algorithm to find the maximum profit. You are in most of the transactions.

Note:
Engage in multiple transactions on the same time (ie, you must sell the stock before you buy again).

Analysis:

According to test instructions. Ask for the maximum profit from trading twice based on the daily stock price. The direct idea is to use the divide-and-conquer approach. From the previous cycle, the array is divided into two stock price series, respectively, to obtain the maximum profit, the two added to the maximum profit of two transactions. Time complexity O (n^2), the submission did indeed expire.

The code is as follows:

Class Solution {public:    Int. profit (vector<int> &vec,int beg,int last)    {        int res=0;        if (beg==last)            return res;        int Min=vec[beg];        for (int i=beg+1;i<=last;++i)        {            if (vec[i]>min)            {                int temp=vec[i]-min;                if (temp>res)                    res=temp;            }            else                min=vec[i];        }        return res;    }    int Maxprofit (vector<int> &prices) {        int res=0;        int n=prices.size ();        if (n<=1)            return res;        for (int i=1;i<n-1;i++)        {            int res1=profit (prices,0,i);            int Res2=profit (prices,i+1,n-1);            if ((res1+res2) >res)                res=res1+res2;        }        return res;    }};

After looking at the discus found in fact can in O (n) time in the first two parts of the maximum profit, the detailed implementation is to use two arrays stored in front of the past and from the back and forth two times the maximum profit per day, and finally get two transactions can obtain the maximum profit.

Detailed code such as the following:

Class Solution {public:    int maxprofit (vector<int> &prices) {        int res=0;        int n=prices.size ();        if (n<=1)            return res;        Vector<int> Front (n,0);        Vector<int> back (n,0);        int minp=prices[0];        int maxp=prices[n-1];        for (int i=1;i<n;i++)        {            if (PRICES[I]>MINP)                front[i]=prices[i]-minp;            else                minp=prices[i];        }        for (int i=n-2;i>=0;i--)        {            if (PRICES[I]>MAXP)                maxp=prices[i];                            Back[i]=max (maxp-prices[i],back[i+1]);            if (front[i]+back[i]>res)                res=front[i]+back[i];                        }        return res;    }};


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Leetcode--best time to Buy and Sell Stock III

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.