C语言 | Leetcode C语言题解之第123题买卖股票的最佳时机III

题目:

题解:

cpp 复制代码
#define max(a, b) ((a) < (b) ? (b) : (a))

int maxProfit(int* prices, int pricesSize) {
    int buy1 = -prices[0], sell1 = 0;
    int buy2 = -prices[0], sell2 = 0;
    for (int i = 1; i < pricesSize; ++i) {
        buy1 = max(buy1, -prices[i]);
        sell1 = max(sell1, buy1 + prices[i]);
        buy2 = max(buy2, sell1 - prices[i]);
        sell2 = max(sell2, buy2 + prices[i]);
    }
    return sell2;
}
相关推荐
让我们一起加油好吗10 分钟前
【数学】数论干货(疑似密码学基础)
c语言·visualstudio·密码学
_Itachi__2 小时前
LeetCode 热题 100 283. 移动零
数据结构·算法·leetcode
鱼不如渔3 小时前
leetcode刷题第十三天——二叉树Ⅲ
linux·算法·leetcode
许科大3 小时前
【笔记ing】C语言补充、组成原理数据表示与汇编实战、操作系统文件实战(高级阶段)
c语言
南宫生4 小时前
力扣每日一题【算法学习day.131】
java·学习·算法·leetcode
时时三省5 小时前
【时时三省】(C语言基础)求多项式1-1/2+1/3-1/4+...+1/99-1/100的值 用C语言表示
c语言
我不是程序猿儿10 小时前
【C】识别一份嵌入式工程文件
c语言·开发语言
武乐乐~14 小时前
欢乐力扣:赎金信
算法·leetcode·职场和发展
子豪-中国机器人14 小时前
2月17日c语言框架
c语言·开发语言
张胤尘15 小时前
C/C++ | 每日一练 (2)
c语言·c++·面试