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
}
相关推荐
实心儿儿6 分钟前
算法7:两个数组的交集
算法·leetcode·职场和发展
sheeta199824 分钟前
LeetCode 每日一题笔记 日期:2025.03.19 题目:3212.统计X和Y频数相等的子矩阵数量
笔记·leetcode·矩阵
Storynone1 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
博风2 小时前
算法:双指针解:盛最多水的容器
算法·leetcode
童话ing2 小时前
【Golang】Golang Map数据结构底层原理
数据结构·golang·哈希算法
阿Y加油吧2 小时前
力扣打卡day05——找到字符串中所有字母异位词、和为K的子数组
leetcode
abant23 小时前
leetcode912 排序算法总结
算法·leetcode·排序算法
liuyao_xianhui3 小时前
优选算法_位运算_只出现一次的数字3_C++
开发语言·数据结构·c++·算法·leetcode·链表·动态规划
GDAL3 小时前
go.mod 文件讲解
golang·go.mod
咕叽吧咔3 小时前
LeetBook乐扣题库 142. 环形链表 II
java·数据结构·leetcode·链表