class Solution { private: int in=0; int sum=0; //保留最大的利润值 public: int maxProfit(vector<int>& prices) { in=prices[0]; for(int i=0;i<prices.size();++i) { if(prices[i]>in) sum=Max(sum,prices[i]-in); else if(prices[i]<in) in=prices[i]; } return sum; } int Max(int x,int y) { return x>y?x:y; } };