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
}
相关推荐
超级皮皮8 小时前
力扣热题之stack
算法·leetcode·职场和发展
YuTaoShao9 小时前
【LeetCode 热题 100】139. 单词拆分——(解法一)记忆化搜索
java·算法·leetcode·职场和发展
圣保罗的大教堂10 小时前
leetcode 1277. 统计全为 1 的正方形子矩阵 中等
leetcode
省四收割者12 小时前
Go语言入门(10)-数组
数据结构·经验分享·笔记·vscode·算法·golang
小欣加油15 小时前
leetcode 904 水果成篮
c++·算法·leetcode
君万15 小时前
【LeetCode每日一题】56. 合并区间
算法·leetcode·golang
墩墩同学15 小时前
【LeetCode题解】LeetCode 287. 寻找重复数
算法·leetcode·二分查找
Elieal15 小时前
Java 链表完全指南:从基础到力扣简单题实战
java·leetcode·链表
小南家的青蛙15 小时前
LeetCode第55题 - 跳跃游戏
算法·leetcode·职场和发展
学历真的很重要17 小时前
Eino 开源框架全景解析 - 以“大模型应用的搭积木指南”方式理解(一)
后端·语言模型·面试·golang·ai编程·eino