Leetcode123:best time to Buy and Sell Stock III

Source: Internet
Author: User

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


Only two stock trading issues.

This problem can be converted to the best time to Buy and Sell Stock I issue.

The core of the two stock trades is the ability to define a trading point, which can be traded before the trading point (the maximum amount of money earned is firstprof), after which a trade can be made (the maximum amount of money earned is secondprof). Then Max (FIRSTPROF+SECONDPROF) is required. However, the time complexity of this method is O (n^2), and the spatial complexity is O (1). Timeout is displayed in Leetcode.

You can use the two-scan method to avoid the double loops above.

Unlike the initial state defined in the best time to Buy and Sell Stock I, A[i] represents the maximum amount of money sold in the first day, this further direct definition A[i] represents the maximum amount of money earned before I day. Minprice represents the lowest price from the No. 0 day to the I-1 day.

A[0]=0. (Initial state)

A[1]=max (Prices[1]-prices[0],a[0])

A[2]=max (Prices[2]-minprice,a[1])

.....

That is A[i]=max (Price[i]-minprice,a[i-1]).

A[0]=0

Another scan scans forward from the array, defining b[i] to represent the maximum amount of money that can be earned from day I to last day N. Maxprice represents the highest price for i+1 days to n days.

B[n]=0. (Initial state)

B[n-1]=max (Maxprice-prices[n-1],b[n])

B[n-2]=max (Maxprice-prices[n-2],b[n-1])

.....

That is B[i]=max (maxprice-prices[i],b[i+1])

B[n]=0

Then the maximum amount of money that can be earned by dividing points on day I is a[i]+b[i]

The solution for the problem is max{a[i]+b[i]}. 0<=i<=n.

The time complexity is O (n), and the spatial complexity is O (n).

Runtime:8ms

Class Solution {Public:int Maxprofit (vector<int>& prices) {int length=prices.size (); if (length==0| |        Length==1) return 0;        int * Ascandmax=new int[length] ();        int minprice=prices[0];        int maxprof=0;            for (int i=1;i<length;i++) {Maxprof=max (maxprof,prices[i]-minprice);            Minprice=min (Minprice,prices[i]);        Ascandmax[i]=maxprof;        } int* descandmax=new Int[length] ();        int maxprice=prices[length-1];        maxprof=0;            for (int i=length-2;i>=0;i--) {Maxprof=max (maxprof,maxprice-prices[i]);            Maxprice=max (Maxprice,prices[i]);        Descandmax[i]=maxprof;        } maxprof=0;        for (int i=0;i<length;i++) {Maxprof=max (maxprof,ascandmax[i]+descandmax[i]);        } delete [] Ascandmax;        delete [] Descandmax;    return maxprof; }};


Leetcode123: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.