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

题目:

题解:

Go 复制代码
func maxProfit(prices []int) int {
    buy1, sell1 := -prices[0], 0
    buy2, sell2 := -prices[0], 0
    for i := 1; i < len(prices); 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
}

func max(a, b int) int {
    if a > b {
        return a
    }
    return b
}
相关推荐
luckycoding42 分钟前
1487. 保证文件名唯一
数据结构·算法·leetcode
big_rabbit05021 小时前
[算法][力扣110]平衡二叉树
数据结构·算法·leetcode
AMoon丶2 小时前
Golang--协程调度
linux·开发语言·后端·golang·go·协程·goroutine
TracyCoder1233 小时前
LeetCode Hot100(70/100)—— 322. 零钱兑换
算法·leetcode·职场和发展
暴躁小师兄数据学院3 小时前
【WEB3.0零基础转行笔记】Go编程篇-第11讲:Gin框架
笔记·golang·web3·区块链·智能合约
AMoon丶3 小时前
Golang--锁
linux·开发语言·数据结构·后端·算法·golang·mutex
x_xbx3 小时前
LeetCode:88. 合并两个有序数组
算法·leetcode·职场和发展
ฅ^•ﻌ•^ฅ13 小时前
LeetCode hot 100(复习c++) 1-15
c++·算法·leetcode
alphaTao3 小时前
LeetCode 每日一题 2026/3/9-2026/3/15
算法·leetcode·职场和发展
Kiyra3 小时前
[特殊字符] LeetCode 做题笔记(二):678. 有效的括号字符串
笔记·算法·leetcode