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
}
相关推荐
圣殿骑士-Khtangc7 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
旖旎夜光8 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
Navigator_Z11 小时前
LeetCode //C - 1192. Critical Connections in a Network
c语言·算法·leetcode
rannn_11111 小时前
【力扣hot100】链表专题下|138、148、23、146
java·算法·leetcode·链表·开发
xzlAwin12 小时前
Win10安装Go语言多版本管理器g工具
开发语言·golang
hanlin0313 小时前
刷题笔记:力扣第189题-轮转数组
笔记·算法·leetcode
琥珀色糖13 小时前
leetcode hot100题(持续更新)移动零(双指针)
算法·leetcode·职场和发展·双指针·移动零
hanlin0314 小时前
动态规划专练:力扣第718、1143题
算法·leetcode·动态规划
FfHUCisI16 小时前
Golang RESTful API 设计原则
开发语言·golang·restful
hanlin0318 小时前
动态规划专练:力扣第1035、392题
算法·leetcode·动态规划