LeetCode NO.122 cpp(3.2)

标签:贪心算法、数组

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    int maxProfit(vector<int>& prices) {
 int i=0,j=1,sum=0;
        if(prices.size()<=1)
        return 0;
        while(j<prices.size()){
            if(prices[j]-prices[i]>0)
                sum+=(prices[j]-prices[i]);
                i++;
                j++;
        }
        return sum;
    }
};

在这里插入图片描述

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

猜你喜欢

转载自blog.csdn.net/weixin_45438011/article/details/104738402
122
今日推荐