Golang | Leetcode Golang题解之第121题买卖股票的最佳时机

题目:

题解:

Go 复制代码
func maxProfit(prices []int) int {
	length:=len(prices)
	if length==0{return 0}
	dp:=make([][]int,length)
	for i:=0;i<length;i++{
		dp[i]=make([]int,2)
	}
	
	dp[0][0]=-prices[0]
	dp[0][1]=0
	for i:=1;i<length;i++{
		dp[i][0]=max(dp[i-1][0],-prices[i])
		dp[i][1]=max(dp[i-1][1],dp[i-1][0]+prices[i])
	}
	return dp[length-1][1]
}

func max(a,b int)int {
    if a>b{
        return a 
    }
    return b 
}
相关推荐
多米Domi0117 小时前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
Lips6118 小时前
2026.1.16力扣刷题
数据结构·算法·leetcode
今天_也很困10 小时前
LeetCode 热题100-15.三数之和
数据结构·算法·leetcode
千金裘换酒10 小时前
LeetCode 数组经典题刷题
算法·leetcode·职场和发展
alphaTao11 小时前
LeetCode 每日一题 2026/1/12-2026/1/18
python·算法·leetcode
sin_hielo11 小时前
leetcode 2943
数据结构·算法·leetcode
程序员-King.13 小时前
day134—快慢指针—环形链表(LeetCode-141)
算法·leetcode·链表·快慢指针
Swift社区13 小时前
LeetCode 376 摆动序列
算法·leetcode·职场和发展
且去填词14 小时前
深入理解 GMP 模型:Go 高并发的基石
开发语言·后端·学习·算法·面试·golang·go
a程序小傲14 小时前
京东Java面试被问:多活数据中心的流量调度和数据同步
java·开发语言·面试·职场和发展·golang·边缘计算