LeetCode NO.121 cpp(2.3)

标签:数组、动态规划

在这里插入图片描述

class Solution {
public:
    int maxProfit(vector<int>& prices) {
      if(prices.empty())
            return 0;
        int profit  = 0;
        int min = prices[0];
        for(int i = 1; i < prices.size(); i++)
        {
            if(min > prices[i])
                min = prices[i];
            else
                profit = max(profit, prices[i] - min);
        }
        return profit;
    }
};

在这里插入图片描述

发布了14 篇原创文章 · 获赞 0 · 访问量 153

猜你喜欢

转载自blog.csdn.net/weixin_45438011/article/details/104598504