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;
}
相关推荐
秋说1 小时前
【PTA数据结构 | C语言版】两枚硬币
c语言·数据结构·算法
qq_513970442 小时前
力扣 hot100 Day37
算法·leetcode
不見星空2 小时前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
☆璇2 小时前
【数据结构】栈和队列
c语言·数据结构
Heartoxx4 小时前
c语言-指针与一维数组
c语言·开发语言·算法
chao_7895 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
GEEK零零七6 小时前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战6 小时前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
秋说6 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
森焱森7 小时前
APM与ChibiOS系统
c语言·单片机·算法·架构·无人机