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
Code:
#include <iostream> #include <cmath> #include <vector>using namespace std;vector<int> prices; int Calculatemax (vector<int> prices) {int i,nmin,nmax,len,ans;len=prices.size (); int *lhs=new Int[len];int *rhs= New Int[len];lhs[0]=0;nmin=prices[0];for (i=1;i<len;i++) {Lhs[i]=max (lhs[i-1],prices[i]-nmin); if (prices[i]< nmin) nmin=prices[i];//cout<< "LHS" <<i<< "<<LHS[I]<<ENDL;} Rhs[len-1]=0;nmax=prices[len-1];for (i=len-2;i>=0;i--) {Rhs[i]=max (rhs[i+1],nmax-prices[i]); if (prices[i]> Nmax) nmax=prices[i];//cout<< "RHS" <<i<< "<<RHS[I]<<ENDL;} Ans=0;for (i=0;i<len;i++) if ((Lhs[i]+rhs[i]) >ans) Ans=lhs[i]+rhs[i];return ans;} int main () {int m,len;while (cin>>m) Prices.push_back (m); Cout<<calculatemax (prices) <<endl;return 0 ;}
The following is a test for removing comments, which can be intuitively
2015 Xiaomi Summer Internship Pen test-tuyere pig-China bull Market (DP)