2015 Xiaomi Summer Internship Pen test-tuyere pig-China bull Market (DP)

Source: Internet
Author: User

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)

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.