solution.cpp 405 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <vector>
#include <algorithm>
using std::max;
using std::min;
using std::vector;

class Solution
{
public:
    int maxProfit(vector<int> &prices)
    {
        if (prices.empty())
            return 0;
        int ret{0}, low{prices[0]};
        for (auto price : prices)
        {
            low = min(low, price);
            ret = max(ret, price - low);
        }
        return ret;
    }
};