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
}
相关推荐
我不是懒洋洋15 分钟前
【经典题目】栈和队列面试题(括号匹配问题、用队列实现栈、设计循环队列、用栈实现队列)
c语言·开发语言·数据结构·算法·leetcode·链表·ecmascript
进击的荆棘1 小时前
递归、搜索与回溯——二叉树中的深搜
数据结构·c++·算法·leetcode·深度优先·dfs
ALex_zry1 小时前
go-zero Redis缓存封装与Model层设计
redis·缓存·golang·气象
人道领域1 小时前
【LeetCode刷题日记】:151翻转字符串的单词(两种解法)
java·开发语言·算法·leetcode·面试
进击的荆棘1 小时前
递归、搜索与回溯——回溯
数据结构·c++·算法·leetcode·dfs
XMYX-016 小时前
17 - Go 通道 Channel 底层原理 + 实战详解
开发语言·golang
cpp_250116 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
加农炮手Jinx17 小时前
LeetCode 146. LRU Cache 题解
算法·leetcode·力扣
加农炮手Jinx17 小时前
LeetCode 128. Longest Consecutive Sequence 题解
算法·leetcode·力扣
旖-旎17 小时前
递归(汉诺塔问题)(1)
c++·学习·算法·leetcode·深度优先·递归