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;
}
相关推荐
云知谷9 分钟前
【经典书籍】C++ Primer 第14类虚函数与多态精华讲解
c语言·开发语言·c++·软件工程·团队开发
爱coding的橙子16 分钟前
每日算法刷题Day77:10.22:leetcode 二叉树bfs18道题,用时3h
算法·leetcode·职场和发展
Swift社区18 分钟前
LeetCode 404:左叶子之和(Sum of Left Leaves)
算法·leetcode·职场和发展
傻童:CPU44 分钟前
C语言需要掌握的基础知识点之递归
c语言·开发语言
一匹电信狗1 小时前
【C++】手搓AVL树
服务器·c++·算法·leetcode·小程序·stl·visual studio
laocooon5238578862 小时前
一个适合新手的训练C题
c语言·开发语言
野蛮人6号3 小时前
力扣热题100道之73矩阵置零
算法·leetcode·矩阵
野蛮人6号3 小时前
力扣热题100道之238除自身以外数组的乘积
算法·leetcode·职场和发展
坚持编程的菜鸟3 小时前
LeetCode每日一题——缀点成线
c语言·算法·leetcode
degen_3 小时前
PEIM安装PPI和调用其他PPI的相关函数
c语言·笔记