美文网首页
LC122 Best Time to Buy and Sell

LC122 Best Time to Buy and Sell

作者: Rookie118 | 来源:发表于2020-08-27 08:41 被阅读0次

本题链接:Best Time to Buy and Sell Stock II

本题标签:ArrayGreedy

本题难度:\color{Green}{Easy}

英文题目 中文题目

方案1:

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

时间复杂度:O ( N )

空间复杂度:O ( 1 )


相关文章

网友评论

      本文标题:LC122 Best Time to Buy and Sell

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