LeetCode 121. Best Time to Buy a

2016-07-13  本文已影响33人  Terence_F

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;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读