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 
}
相关推荐
小羊在睡觉2 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
sheeta19983 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode
_日拱一卒3 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
小白兔奶糖ovo6 小时前
【Leetcode】231. 2的幂
linux·算法·leetcode
过期动态7 小时前
【LeetCode 热题 100】接雨水
java·数据结构·算法·leetcode·职场和发展
ruxingli11 小时前
Golang iota详解
开发语言·后端·golang
圣保罗的大教堂12 小时前
leetcode 3300. 替换为数位和以后的最小元素 简单
leetcode
sheeta199812 小时前
LeetCode 每日一题笔记 日期:2026.05.27 题目:3121. 统计特殊字母的数量 II
笔记·算法·leetcode
Tisfy12 小时前
LeetCode 3300.替换为数位和以后的最小元素:一次遍历
数学·算法·leetcode·模拟
暗冰ཏོ13 小时前
Go 语言从入门到后端项目实战完整指南
开发语言·后端·golang·go·go语言