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;
}
相关推荐
野生风长15 小时前
从零开始的C语言:文件操作与数据管理(下)(fseek,ftell,rewind,文件的编译和链接)
android·java·c语言·开发语言·visual studio
资深web全栈开发15 小时前
LeetCode 2054:两个最好的不重叠活动 —— 从暴力到优化的完整思路
算法·leetcode
IT方大同15 小时前
数组的初始化与使用
c语言·数据结构·算法
im_AMBER15 小时前
Leetcode 84 水果成篮 | 删除子数组的最大得分
数据结构·c++·笔记·学习·算法·leetcode·哈希算法
长安er15 小时前
LeetCode 124/543 树形DP
算法·leetcode·二叉树·动态规划·回溯
杜子不疼.15 小时前
【LeetCode 153 & 173_二分查找】寻找旋转排序数组中的最小值 & 缺失的数字
算法·leetcode·职场和发展
CSDN_RTKLIB15 小时前
【LeetCode 热题 HOT 100】两数之和
算法·leetcode·职场和发展
福尔摩斯张15 小时前
深入理解C/C++套接字编程:从基础到实践(超详细)
linux·c语言·开发语言·c++·tcp/ip·udp
Tisfy15 小时前
LeetCode 2054.两个最好的不重叠活动:二分查找
算法·leetcode·二分查找·题解
黎雁·泠崖15 小时前
C 语言字符串入门:字符函数 + strlen 精讲(从使用到模拟实现)
c语言·开发语言