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
}
相关推荐
玛卡巴卡ldf29 分钟前
【LeetCode 手撕算法】(矩阵)73-矩阵置零、54-螺旋矩阵(贪吃蛇)、48-旋转图像
java·数据结构·算法·leetcode·力扣
雪木木34 分钟前
刷题:力扣热题100--滑动窗口(Day03)
算法·leetcode
We་ct1 小时前
LeetCode 153. 旋转排序数组找最小值:二分最优思路
前端·算法·leetcode·typescript·二分·数组
拾荒的路由1 小时前
HOT100DAY9记录用
数据结构·算法·leetcode
沙雕不是雕又菜又爱玩1 小时前
leetcode第7题 整数反转(C++)
算法·leetcode
ruxingli2 小时前
GoLang channel管道
开发语言·后端·golang
_DCG_2 小时前
go第一个工程安装过程与问题汇总
开发语言·后端·golang
Tisfy2 小时前
LeetCode 3548.等和矩阵分割 II:矩阵旋转 + 哈希表
leetcode·矩阵·散列表·题解·哈希表·矩阵旋转
abant22 小时前
leetcode 148 排序链表 归并终极形态
算法·leetcode·链表
苦瓜小生2 小时前
【Leetcode Hot 100刷题路线】| 找工作速刷 | 第23题 - [49] - 字母异位词分组
算法·leetcode·职场和发展