Leetcode121:best time to Buy and Sell Stock

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.

If you were-permitted-to-complete at most one transaction (ie, buy one and sell one share of the stock), design an AL Gorithm to find the maximum profit.

The problem of stock trading, different state transfer equations can be used to analyze this problem. First, we look at the solution of the maximal and problem of the final transformation into sub-arrays.

The initial state A[i] can be defined as the day I sell the most money you can earn. Set the array prices=[2,3,1,4,5,3,6], starting from day No. 0

You can calculate the difference between two adjacent items in the above array first, subprices=[1,-2,3,1,-2,3]

A[1] means the 1th day to sell the most money you can earn, a[1]=1

a[2] means the 2nd day selling can earn the most money , a[2]=a[1]+ (-2) =-1

a[3" means the 3rd day selling can earn the most money ,a[3]=3

a[4" means the 4th day selling can earn the most money ,a[4]=a[3]+1=4

A[5] means the 5th day to sell the most money you can earn , a[5]=a[4]+ ( -2)

.....

Then the state transfer equation:

A[i]=max{a[i-1],0}+subprices[i]

The solution to the problem is max{a[i]}

The code for the above analysis is as follows:

Runtime:8ms

Class Solution {public:    int Maxprofit (vector<int>& prices) {        int length=prices.size ();        if (length==0| | length==1)            return 0;        int Last=max (prices[1]-prices[0],0);        int maxresult=last;        for (int i=2;i<length;i++)        {            Last=max (last,0) +prices[i]-prices[i-1];            if (last>maxresult)                maxresult=last;        }        return maxresult;}    ;

You can also count the lowest prices for those days, the highest price for that day, and then subtract the lowest value from the highest value.

The initial state can be defined as the maximum amount of money that can be earned on the day I sell. set Array prices=[2,3,1,4,5,3,6]

But the state transition equation can be defined like this:

A[i]=prices[i]-min{a[j]},1<=j<i

The solution of the problem is also max{a[i]}

Class Solution {public:    int Maxprofit (vector<int>& prices) {        if (prices.size () <1)          return 0;        int minstock=prices[0];        int maxprofit=0;        for (int i=0;i<prices.size (); i++)        {            Maxprofit=max (maxprofit,prices[i]-minstock);            Minstock=min (Minstock,prices[i]);        }        return maxprofit;}    ;



Leetcode121:best time to Buy and Sell Stock

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.