LeetCode 121. Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock

这是一道easy题,考察Dynamic programming。因为刚打开leetcode 309, Best Time to Buy and Sell Stock with Cooldown, 所以就把leetcode121这道题拿出来复习了一下

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int max_profit = 0;
        int min_price = INT_MAX;
        for (auto p: prices) {
            min_price = min(min_price, p);
            max_profit = max(max_profit, p - min_price);
        }
        return max_profit;
    }
};

推荐阅读更多精彩内容