121.Best Time to Buy and Sell Stock

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        if (n==0){
            return 0;
        }
        int res = 0;
        int mmin = prices[0];
        for (int i=1;i<n;i++){
            res = max(res,prices[i]-mmin);
            mmin = min(mmin,prices[i]);
        }
        return res;
    }
};
posted @ 2019-04-09 15:25  JohnRed  阅读(72)  评论(0编辑  收藏  举报