天天看點

19、 2015小米暑期實習筆試題--風口的豬-中國牛市

class Solution {
public:
    /**
     * 計算你能獲得的最大收益
     * 
     * @param prices Prices[i]即第i天的股價
     * @return 整型
     */
    int max_second(vector<int> a,int x,int y)
    {     int min1=a[x];
          int max1=0;
        for(int j=x;j<y;j++)
        { if(a[j]-min1>max1) max1=a[j]-min1;
            if(a[j]<min1) min1=a[j];
        }
     
     return max1;
        
        
    }
    int calculateMax(vector<int> prices) {
       int t;
        int s=0;
        for(int i=0;i<prices.size();i++)
        {   t=max_second(prices,0,i)+max_second(prices,i,prices.size());
          if(t>s) s=t;}
        return s;
        
    }
};