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
}
相关推荐
chao_7892 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
海奥华24 小时前
go中的接口返回设计思想
开发语言·后端·golang
岁忧4 小时前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z5 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
飞川撸码8 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
_Itachi__16 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
roman_日积跬步-终至千里16 小时前
【Go语言基础【14】】defer与异常处理(panic、recover)
golang
孔令飞17 小时前
Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践
ai·云原生·容器·golang·kubernetes
chao_78917 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表