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
}
相关推荐
码途行者10 小时前
每天10分钟学Go:第17课 接口interface-只规定“能做什么“,不管它是什么
开发语言·后端·golang
利威尔xu20 小时前
2026-11-24-Go限流器实战
后端·golang
Navigator_Z1 天前
LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers
c语言·算法·leetcode
码途行者1 天前
每天10分钟学Go:第21课 错误
开发语言·后端·golang
码途行者1 天前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
wy_hhxx2 天前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者2 天前
每天10分钟学Go:第20课 泛型
c++·算法·golang
codeejun2 天前
每日一Go·MySQL-20、SQL 性能调优实战总结
sql·mysql·golang
AIgorithmGEEK2 天前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
Navigator_Z2 天前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode