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;
}
相关推荐
wuhen_n4 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo4 小时前
leetcode 2483
数据结构·算法·leetcode
sevenez5 小时前
Vibe Coding 实战笔记:从“修好了C坏了AB”到企业级数据库架构重构
c语言·笔记·数据库架构
一路往蓝-Anbo5 小时前
【第20期】延时的艺术:HAL_Delay vs vTaskDelay
c语言·数据结构·stm32·单片机·嵌入式硬件
wuhen_n6 小时前
LeetCode -- 1:两数之和(简单)
javascript·算法·leetcode·职场和发展
就不掉头发6 小时前
I/O复用
运维·服务器·c语言·开发语言
Jeremy爱编码8 小时前
leetcode课程表
算法·leetcode·职场和发展
努力学算法的蒟蒻8 小时前
day46(12.27)——leetcode面试经典150
算法·leetcode·面试
ComputerInBook9 小时前
函数调用栈帧分析(Windows平台)
c语言·windows·编译原理·汇编语言·c++语言
元亓亓亓10 小时前
LeetCode热题100--152. 乘积最大子数组--中等
算法·leetcode·职场和发展