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
}
相关推荐
何以解忧,唯有..1 天前
Go语言循环语句详解:for、range与循环控制
开发语言·算法·golang
踏着七彩祥云的小丑1 天前
Go学习第9天:并发编程 + 文件操作 + 正则表达式
学习·golang·正则表达式·go
想吃火锅10051 天前
【leetcode】121.买卖股票的最佳时机js/c++
算法·leetcode·职场和发展
JCGKS1 天前
Go `init` 函数:包初始化顺序到底是怎样的
golang·init·init执行顺序
何以解忧,唯有..1 天前
Go语言中的const:常量声明与iota枚举详解
java·开发语言·golang
凌波粒1 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
退休倒计时1 天前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript
小欣加油1 天前
leetcode3612 用特殊操作处理字符串I
数据结构·c++·算法·leetcode·职场和发展
凌波粒1 天前
LeetCode--90.子集II(回溯算法)
数据结构·算法·leetcode
凌波粒1 天前
LeetCode--46.全排列(回溯算法)
数据结构·算法·leetcode