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
}
相关推荐
bbq粉刷匠1 天前
力扣--两数之和(Java)
java·leetcode
树在风中摇曳1 天前
LeetCode 1658 | 将 x 减到 0 的最小操作数(C语言滑动窗口解法)
c语言·算法·leetcode
.柒宇.1 天前
力扣hoT100之找到字符串中所有字母异位词(java版)
java·数据结构·算法·leetcode
YoungHong19921 天前
面试经典150题[063]:删除链表的倒数第 N 个结点(LeetCode 19)
leetcode·链表·面试
青山的青衫1 天前
【前后缀】Leetcode hot 100
java·算法·leetcode
q***18841 天前
搭建Golang gRPC环境:protoc、protoc-gen-go 和 protoc-gen-go-grpc 工具安装教程
开发语言·后端·golang
啊吧怪不啊吧1 天前
二分查找算法介绍及使用
数据结构·算法·leetcode
Kuo-Teng1 天前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
码上淘金2 天前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json
橘颂TA2 天前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++