best time to Buy and Sell Stock III

Source: Internet
Author: User

best time to Buy and Sell Stock IIITotal accepted:30820 Total submissions:130535my submissions

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

1#include <vector>2#include <iostream>3 using namespacestd;4 5 6 intMain ()7 {8     intprisize, tem,i;9vector<int>pri;Ten      while(Cin >>prisize) One     { A pri.clear (); -          for(i =0; i < prisize; ++i) -         { theCIN >>tem; - Pri.push_back (TEM); -         } -  +         if(prisize<=1)  -         { +cout <<0<<Endl; A             Continue; at         } -         if(prisize==2)   -         { -             intTEM = pri[1]>pri[0]?pri[1]-pri[0]:0; -cout << tem <<Endl; -             Continue; in         } -  to  +         //in the first case, I bought it only once. -         //1, from left to right traverse save min Price min, Judge Pri[i]-min > maxprice?, update the current maximum profit Maxprice the         //2, from left to right traverse save Max Price Max, Judge Max-pri[i] > maxprice?, update Maxprice *  $         //in the second case, I bought it two times.Panax Notoginseng         //3, max (d) = Maxprice (0 <= D <= i) + maxprice (I < D < prisize) -         //D traverse between 0 < D < priSize-1, interpret Max (d) > Maxprice, update maxprice the         //results of Maxprice (0 <= D <= i) and Maxprice (I < D < prisize) +         //can be recorded in the Maxfromleft,maxfromright in the 1th and 2 steps in advance. A         //4, maxfromleft[prisize-1] = = maxfromright[0] = = The maximum profit to buy only once the         //use one of them to compare the size with the 3rd, Maxprice, and get the result +  -  $         //algorithmic complexity O (n-1 + n-1 + n-2) = O (3n-4) = O (n) $  -vector<int> Maxfromleft (prisize,0);  -vector<int> Maxfromright (prisize,0); the         intMIN = Int_max, max=int_min, Maxprice =Int_min,pritem; -          for(intI=0; i<prisize;i++)Wuyi         { the             //Update the minimum price starting from the left -             if(pri[i]<MIN) WuMIN =Pri[i]; -Pritem = pri[i]-MIN; About  $             //Update current maximum profit -             if(Pritem >maxprice) -Maxprice =Pritem; -  A             //Save Maxprice (0 <= D <= i) +Maxfromleft[i] =Maxprice; the         } -  $Maxprice =int_min; the          for(inti=prisize-1; i>=0;--i) the         { the             //Update the maximum price starting from the right the             if(pri[i]>MAX) -MAX =Pri[i]; inPritem = MAX-Pri[i]; the             //Update current maximum profit the             if(Pritem >maxprice) AboutMaxprice =Pritem; the             //Save Maxprice (i < D < prisize) theMaxfromright[i] =Maxprice; the         } +  -Maxprice =int_min; the          for(intI=0; i<prisize-1; i++){Bayi             //Update current maximum profit thePritem = maxfromleft[i]+maxfromright[i+1]; the             if(Pritem > Maxprice) Maxprice =Pritem; -         } -  the         //maxfromleft[prisize-1] and maxprice compare size, Draw results the         if(maxfromleft[prisize-1] >maxprice) theMaxprice = maxfromright[0]; the  -cout << Maxprice <<Endl; the     } the  the     return 0 ;94}

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.