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
}
相关推荐
海琴烟Sunshine1 小时前
leetcode 168. Excel 表列名称 python
python·算法·leetcode
1白天的黑夜12 小时前
递归-21.合并两个有序链表-力扣(LeetCode)
c++·leetcode·链表·递归
坚持编程的菜鸟2 小时前
LeetCode每日一题——在区间范围内统计奇数数目
c语言·算法·leetcode
云闲不收2 小时前
golang的一些技巧
开发语言·后端·golang
前进之路92 小时前
Leetcode每日一练--35
算法·leetcode
微笑尅乐3 小时前
三种思路彻底掌握 BST 判断(递归与迭代全解析)——力扣98.验证二叉搜索树
算法·leetcode·职场和发展
白云千载尽5 小时前
leetcode 2598 执行操作后最大MEX
算法·leetcode·职场和发展
h7997106 小时前
go资深之路笔记(八) 基准测试
golang·压力测试
Seven977 小时前
剑指offer-35、数组中的逆序对
java·leetcode
熬了夜的程序员8 小时前
【LeetCode】74. 搜索二维矩阵
线性代数·算法·leetcode·职场和发展·矩阵·深度优先·动态规划