121. Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        int low = INT_MAX;
        for (auto i : prices) {
            if (i < low)
                low = i;
            if (i - low > res)
                res = i - low;
        }
        return res;
    }
};

 

posted @ 2018-11-19 13:51  JTechRoad  阅读(75)  评论(0编辑  收藏  举报