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
}
相关推荐
AlenTech6 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
sin_hielo7 小时前
leetcode 1161(BFS)
数据结构·算法·leetcode
iAkuya8 小时前
(leetcode)力扣100 34合并K个升序链表(排序,分治合并,优先队列)
算法·leetcode·链表
放荡不羁的野指针9 小时前
leetcode150题-字符串
数据结构·算法·leetcode
橘颂TA9 小时前
【剑斩OFFER】算法的暴力美学——存在重复元素Ⅱ
算法·leetcode·哈希算法·散列表·结构与算法
cg501710 小时前
力扣数据库——组合两个表
sql·算法·leetcode
2501_9417987310 小时前
面向微服务分布式事务补偿与最终一致性的互联网系统高可用设计与多语言工程实践分享
leetcode·模拟退火算法
ada7_11 小时前
LeetCode(python)22.括号生成
开发语言·数据结构·python·算法·leetcode·职场和发展
YuTaoShao11 小时前
【LeetCode 每日一题】1161. 最大层内元素和——BFS
算法·leetcode·宽度优先
黛色正浓11 小时前
leetCode-热题100-子串合集(JavaScript)
javascript·算法·leetcode