剑指offer 63:股票的最大利润

在这里插入图片描述
思路
与其说是动态规划,倒不如说是贪心。

class Solution {
    
    
public:
    int maxProfit(vector<int>& prices) {
    
    
        int res = 0;
        int minPrice = INT_MAX;
        for (int price : prices) {
    
    
            minPrice = min(minPrice, price);
            res = max(res, price - minPrice);
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_44537258/article/details/114042925