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
}
相关推荐
203号居民3 小时前
LeetCode hot 100 — 141. 环形链表2
算法·leetcode·链表
xcLeigh3 小时前
Go入门:整数类型的溢出与安全边界
java·安全·golang
玖玥拾3 小时前
LeetCode 202 快乐数
算法·leetcode
Tim_104 小时前
【LeetCode】29、两数相除
算法·leetcode·职场和发展
lvwangshu7 小时前
P6534 [COCI 2015/2016 #1] UZASTOPNI 等差树列 题解
动态规划·图论·题解·性质题
ttwuai9 小时前
Go 后台附件迁到对象存储后,path、cdnUrl 和 tenant_id 怎么一起验?
开发语言·后端·golang
ttwuai10 小时前
Go 后台清空操作日志失败,权限和无 WHERE 删除怎么排查?
开发语言·后端·golang
gsls20080811 小时前
OpsKat封装MCP:用 Go 标准库把运维 CLI 变成 AI 的“手“
运维·人工智能·golang·mcp
土司大王11 小时前
LeetCode hot100——缺失的第一个正数
数据结构·算法·leetcode
Cccp.12312 小时前
【leetcode】(二)认识O(NlogN)的排序
算法·leetcode