(written) The pig of the tuyere-China bull market

Source: Internet
Author: User

Topic:

The pigs can fly under the tuyere. The bull market in China today is a "missed seven years". Give you an opportunity to review the history of a stock, known for a continuous N-day price movement, in the length of an integer array of n, the array of elements I (Prices[i]) represents the stock price of the first day. Suppose you don't have a stock at first, but you have the opportunity to buy 1 shares and then sell 1 shares at most two times, and make sure you have no stock before you buy. If two trading opportunities are abandoned, the yield is 0. Design algorithms to calculate the maximum benefits you can get. Input value range: 2<=n<=100,0<=prices[i]<=100

Input Example:

3,8,5,1,7,8

Output Example:

12
Ideas: 1, dynamic planning

I denotes day I, k denotes the K-th transaction

    • State transition equation:
      • Pmax = max{F (j,k-1) +prices[i]-prices[j]} (0<=j<i)}
      • F (i,k) = max{f (i-1,k), Pmax}
      • i.e. f (i,k) = max{f (i-1,k), max {f (j,k-1) + prices[i]-prices[j] | 0<=j<i}}
    • F (i-1,k) means I do not trade for the first day
    • max{f (j,k-1) +prices[i]-prices[j]} (0<=j<i)} represents the day I sell, and the purchase of this transaction should come from Day J, F (j,k-1) +prices[i]-prices[j],0<=j< I, take the maximum value.
    • max{f (j,k-1) +prices[i]-prices[j]} (0<=j<i)}, this step can be optimized by monotone queue, can refer to: http://www.cnblogs.com/ka200812/archive /2012/07/11/2585950.html, to put it simply, is to handle the part of the equation that contains J separately, i.e. Max (f (j,k-1)-prices[j])
2, prefix, suffix array

The maximum profit of 0 or 1 trades is calculated by prefix array A.

The maximum profit of 0 or 1 trades is calculated by the suffix array b;

By traversing 0-n, calculating Max (A[i]+b[i]) is the maximum benefit;

Code: 1, simple dynamic planning
classSolution { Public:    /** * calculate the maximum profit you can earn * * @param prices Prices[i] that is the stock price of the first day * @return integer type*/    intKtransaction (vector<int> Prices,intk) {        intn=prices.size (); Vector<vector<int> > DP (n,vector<int> (k +1,0)); intMX;  for(intI=1; i<n;i++){             for(intt=1; t<=k;t++) {MX=dp[i-1][t];  for(intj=i-1; j>=0; j--) MX=max (mx,dp[j][t-1]+prices[i]-Prices[j]); Dp[i][t]=MX; }        }        returndp[n-1][k]; }         intCalculatemax (vector<int>prices) {    returnKtransaction (Prices,2); }};
2. Dynamic programming of monotone queue optimization
classSolution { Public:    /** * calculate the maximum profit you can earn * * @param prices Prices[i] that is the stock price of the first day * @return integer type*/    intKtransaction (vector<int> Prices,intk) {        intn=prices.size (); Vector<vector<int> > DP (n,vector<int> (k +1,0)); Vector<int> PMax (k +1,0); intMX;  for(intI=0; i<=k;i++) Pmax[i]=-prices[0];  for(intI=1; i<n;i++) {pmax[0]=max (pmax[0],dp[i][0]-prices[0]);  for(intt=1; t<=k;t++) {MX=dp[i-1][t]; MX=max (mx,pmax[t-1]+Prices[i]); Pmax[t-1]=max (pmax[t-1],dp[i][t-1]-Prices[i]); Dp[i][t]=MX; }        }        returndp[n-1][k]; }         intCalculatemax (vector<int>prices) {    returnKtransaction (Prices,2); }};
3. prefix suffix array
classSolution { Public:    /** * calculate the maximum profit you can earn * * @param prices Prices[i] that is the stock price of the first day * @return integer type*/    intCalculatemax (vector<int>prices) {        intn=prices.size (); Vector<int>Leftmax (n); Vector<int>Rightmax (n); intlmin=prices[0]; leftmax[0]=0;  for(intI=0; i<n;i++) {lmin=min (lmin,prices[i]); Leftmax[i]=max (leftmax[i-1],prices[i]-lmin); }        intrmax=prices[n-1]; Rightmax[n-1]=0;  for(inti=n-2; i>=0; i--) {Rmax=Max (rmax,prices[i]); Rightmax[i]=max (rightmax[i+1],rmax-Prices[i]); }        intpmax=0;  for(intI=0; i<n;i++) Pmax=max (pmax,leftmax[i]+Rightmax[i]); returnPmax; }};

(written) The pig of the tuyere-China bull market

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.