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
}
相关推荐
mmmmath_37 小时前
LeetCode.541.反转字符串II
数据结构·算法·leetcode
Navigator_Z7 小时前
LeetCode //MySQL - 1251. Average Selling Price
c语言·算法·leetcode
参.商.9 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
虚无的纽扣12 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
传奇开心果编程14 小时前
【Go入门练中学】第2课:条件与循环
后端·学习·golang
圣保罗的大教堂15 小时前
leetcode 3742. 网格中得分最大的路径 中等
leetcode
我不会起名字32216 小时前
一天一道算法题(35):电话号码的字母组合
java·数据结构·后端·python·leetcode·go·回溯
圣保罗的大教堂17 小时前
leetcode 2033. 获取单值网格的最小操作数 中等
leetcode
6Hzlia17 小时前
【Classic 150 刷题计划】 LeetCode 242. 有效的字母异位词 | C++ 哈希计数与严密防线
c++·算法·leetcode
wabs66618 小时前
关于二叉树【力扣101.对称二叉树的思考】
数据结构·c++·算法·leetcode·二叉树