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
}
相关推荐
晴空了无痕2 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
qq_452396235 小时前
第五篇:《接口与错误处理:Go 的哲学》
开发语言·后端·golang
程序猿乐锅6 小时前
【数据结构与算法 | 第六篇】力扣1109,1094差分数组
java·算法·leetcode
布朗克1687 小时前
Go 入门到精通-29-测试与基准
开发语言·后端·golang·测试与基准
hold?fish:palm9 小时前
9 找到字符串中所有字母异位词
c++·算法·leetcode
Sw1zzle10 小时前
算法入门(六):贪心算法 - 基础入门(Leetcode 121/455/860/376/738)
算法·leetcode·贪心算法
青山木10 小时前
Hot 100 --- 岛屿数量
java·数据结构·算法·leetcode·深度优先·广度优先
啦啦啦啦啦zzzz11 小时前
算法:回溯算法
c++·算法·leetcode
止语Lab13 小时前
从 PHP 到 Go:真正迁移的是复杂度的归属
android·golang·php
小肝一下13 小时前
3. 单链表
c语言·数据结构·c++·算法·leetcode·链表·dijkstra