Leetcode—122.买卖股票的最佳时机II【中等】

2023每日刷题(二十八)

Leetcode—122.买卖股票的最佳时机II

Leetcode—122.买卖股票的最佳时机II【中等】_第1张图片

实现代码

int maxProfit(int* prices, int pricesSize) {
    int totalProfit = 0;
    if(pricesSize <= 1) {
        return 0;
    }
    for(int i = 1; i < pricesSize; i++) {
        if(prices[i] - prices[i - 1] > 0) {
            totalProfit += prices[i] - prices[i - 1];
        }
    }
    return totalProfit;
}

运行结果

Leetcode—122.买卖股票的最佳时机II【中等】_第2张图片

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

你可能感兴趣的:(LeetCode刷题,leetcode,算法,职场和发展,贪心,c语言,经验分享)