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
}
相关推荐
Nuanyt12 小时前
LeetCode Hot 100 链表专题(五)· 2 题(Java)
java·算法·leetcode·链表
圣保罗的大教堂16 小时前
leetcode 3736. 最小操作次数使数组元素相等 III 简单
leetcode
文丑颜不良啊16 小时前
LeetCode1881~1990题目解析
java·leetcode
文丑颜不良啊19 小时前
LeetCode1861~1880题目解析
java·leetcode
文丑颜不良啊20 小时前
LeetCode1921~1940题目解析
java·leetcode
一去不复返的通信er1 天前
排序 + 双指针:力扣15三数之和
算法·leetcode
文丑颜不良啊1 天前
LeetCode1901~1920题目解析
java·leetcode
Pniubi1 天前
力扣3无重复字符的最长字串(滑动窗口)
算法·leetcode·职场和发展
Zenova EdgeOS1 天前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
JavaPub-rodert2 天前
从零搭建通用管理后台:ShiyuAdmin 的 Go、React 与 RBAC 实践
开发语言·react.js·golang