美文网首页
LeetCode 121. Best Time to Buy a

LeetCode 121. Best Time to Buy a

作者: Terence_F | 来源:发表于2016-07-13 09:54 被阅读33次

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;
    }
};

相关文章

网友评论

      本文标题:LeetCode 121. Best Time to Buy a

      本文链接:https://www.haomeiwen.com/subject/immsjttx.html