剑指OFFER----面试题63. 股票的最大利润

链接:https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof/、

代码

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int res = 0;
        if (prices.empty()) return 0;
        for (int i = 1, minv = prices[0]; i < prices.size(); ++i) {
            res = max(res, prices[i] - minv);
            minv = min(minv, prices[i]);
        }
        return res;
    }
};
posted @ 2020-03-14 14:20  景云ⁿ  阅读(99)  评论(0编辑  收藏  举报