Leetcode|中等|股票|122. 买卖股票的最佳时机 II

2021-09-18 16:36:21 浏览数 (1)

1 贪心算法

代码语言:javascript复制
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int curDiff = 0;
        int money = 0;
        for (int i = 0; i < prices.size(); i  ) {
            if (i == 0) continue;
            curDiff = prices[i] - prices[i-1];
            if (curDiff > 0) 
                money  = curDiff;
        }
        return money;
    }
};

致谢

图片来源于「代码随想录」公众号,欢迎大家关注这位大佬的公号

0 人点赞