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
}
相关推荐
ada7_3 小时前
LeetCode(python)108.将有序数组转换为二叉搜索树
数据结构·python·算法·leetcode
独自破碎E4 小时前
加油站环路问题
java·开发语言·算法·leetcode
Swift社区4 小时前
LeetCode 445 - 两数相加 II
算法·leetcode·职场和发展
墨染点香4 小时前
LeetCode 刷题【187. 重复的DNA序列】
算法·leetcode·职场和发展
2401_841495645 小时前
【LeetCode刷题】最大子数组和
数据结构·python·算法·leetcode·动态规划·最大值·最大子数组和
鹿角片ljp5 小时前
力扣101.判断对称二叉树-推荐掌握递归
算法·leetcode·职场和发展
2401_841495646 小时前
【LeetCode刷题】最小覆盖字串
数据结构·python·算法·leetcode·字符串·双指针·滑动窗口算法
qk学算法7 小时前
力扣算法——二分最大值最小值
数据结构·算法·leetcode
努力学算法的蒟蒻7 小时前
day28(12.8)——leetcode面试经典150
算法·leetcode·面试