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
}
相关推荐
夏鹏今天学习了吗6 小时前
【LeetCode热题100(87/100)】最小路径和
算法·leetcode·职场和发展
Lips6118 小时前
2026.1.20力扣刷题笔记
笔记·算法·leetcode
钟离墨笺9 小时前
Go语言--2go基础-->基本数据类型
开发语言·前端·后端·golang
shhpeng9 小时前
go gtk 开发入门
golang
鱼跃鹰飞9 小时前
Leetcode347:前K个高频元素
数据结构·算法·leetcode·面试
钟离墨笺11 小时前
Go语言--2go基础-->map
开发语言·后端·golang
Tony Bai11 小时前
Go 语言的“魔法”时刻:如何用 -toolexec 实现零侵入式自动插桩?
开发语言·后端·golang
52Hz11812 小时前
力扣24.两两交换链表中的节点、25.K个一组反转链表
算法·leetcode·链表
老鼠只爱大米12 小时前
LeetCode经典算法面试题 #160:相交链表(双指针法、长度差法等多种方法详细解析)
算法·leetcode·链表·双指针·相交链表·长度差法
老鼠只爱大米12 小时前
LeetCode经典算法面试题 #84:柱状图中最大的矩形(单调栈、分治法等四种方法详细解析)
算法·leetcode·动态规划·单调栈·分治法·柱状图最大矩形