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
}
相关推荐
失去的青春---夕阳下的奔跑2 小时前
560. 和为 K 的子数组
数据结构·算法·leetcode
m0_629494733 小时前
LeetCode 热题 100-----25.回文链表
数据结构·算法·leetcode·链表
吃着火锅x唱着歌5 小时前
LeetCode 1019.链表中的下一个更大节点
算法·leetcode·链表
凌波粒6 小时前
LeetCode--404.左叶子之和(二叉树)
算法·leetcode·职场和发展
绝知此事6 小时前
【算法突围 03】核心算法思想:分治/递归/动态规划与 LeetCode 高频真题解析
算法·leetcode·面试·动态规划
宇明一不急6 小时前
go 链表 (标准库实现)
开发语言·链表·golang
阿Y加油吧8 小时前
两道字符串 DP 模板题复盘:最长公共子序列 & 编辑距离
leetcode
~|Bernard|8 小时前
GO语言中哪些类型是可比较类型的(==和!=)
开发语言·后端·golang
我爱cope8 小时前
【力扣hot100:76. 最小覆盖子串】
算法·leetcode·职场和发展
sheeta19988 小时前
LeetCode 每日一题笔记 日期:2026.05.20 题目:2657. 找到前缀公共数组
笔记·算法·leetcode