Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int size = prices.size();
        if( size==0 || size==1)
            return 0;
        int minPrice = prices[0];
        int curMax = 0;
       for(int i = 1;i < size; ++i)
       {
           if(prices[i] > minPrice)
                curMax = max( curMax , prices[i] - minPrice);
            else if( prices[i] < minPrice)
                minPrice = prices[i];
       }
       return curMax;
        
    }
};

 

posted @ 2014-11-25 10:21  dupuleng  阅读(79)  评论(0编辑  收藏  举报