[Leetcode] best time to Buy and Sell Stock III

Source: Internet
Author: User

best time to Buy and Sell Stock III

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).

Problem Solving Ideas:

Test instructions for a given daily stock price, what is the maximum profit for a maximum of two trades?

My idea is that the maximum profit for a single trade is P1 (as previously calculated), up to two trades for P2, and then back to Max (P1, P2).

For two transactions, it can be considered as two times a transaction. Each trade we are in the crest/trough, so we only need to divide in the crest/trough each time.

The code is as follows:

Class Solution {Public:int Maxprofit (vector<int>& prices) {vector<int> uniqueprices = uniqueNe        Ighbor (prices);        int len = Uniqueprices.size ();        if (len = = 0 | | len = = 1) {return 0; } int oneprofit = Maxprofithelper (uniqueprices, 0, len-1);        One TRADE//divided into two trades, each trough (or crest cut) int twoprofit = 0; for (int i=1; i<len-1; i++) {if (Uniqueprices[i]<uniqueprices[i-1] && uniqueprices[i]<uniquepric Es[i+1]) {twoprofit = max (Twoprofit, Maxprofithelper (uniqueprices, 0, i) + maxprofithelper (uniqueprices, I            , len-1));    }} return Max (Oneprofit, Twoprofit); }//Represents the maximum profit from start to end of the transaction at most once int maxprofithelper (vector<int>& prices, int start, int end) {if (        Start>=end) {return 0;        } int result = 0;        int minprices = Prices[start]; for (int i=start+1; i<=end; i++) {minprices = min (Prices[i], minprices);        result = Max (result, prices[i]-minprices);    } return result;        } vector<int> Uniqueneighbor (vector<int>& prices) {vector<int> result;        int len = Prices.size ();        if (len = = 0) {return result;        } result.push_back (Prices[0]); for (int i=1; i<len; i++) {if (Result[result.size () -1]!=prices[i]) {result.push_back (prices[i])            ;    }} return result; }};
Note that the first day of the adjacent price is synthesized one day (no effect on the result), and then the calculation can be greatly reduced. But the worst time complexity is O (n^2), and the spatial complexity is O (1).

Another method is the dynamic programming method. The code is as follows:

Class Solution {public:    int Maxprofit (vector<int>& prices) {        int len = Prices.size ();        if (len<=1) {            return 0;        }        Vector<int> left (len, 0);        Vector<int> right (len, 0);                Left[0] = 0;        int minprices = prices[0];        for (int i=1; i<len; i++) {            minprices = min (prices[i], minprices);            Left[i] = max (left[i-1], prices[i]-minprices);        }                Right[len-1] = 0;        int maxprices = prices[len-1];        for (int i=len-2; i>=0; i--) {            maxprices = max (Prices[i], maxprices);            Right[i] = max (right[i+1], maxprices-prices[i]);        }                int maxprofit = 0;                for (int i=0; i<len; i++) {            maxprofit = max (Maxprofit, left[i] + right[i]);        }                return maxprofit;}    ;



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not 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.