121. Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() == 0) return 0;
        int res = 0, _min = prices[0];
        for (auto p : prices) {
            if (p < _min)
                _min = p;
            res = max(res, p - _min);
        }
        return res;
    }
};

 

posted @ 2018-05-20 14:09  JTechRoad  阅读(82)  评论(0编辑  收藏  举报