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 分钟前
Go 语言-->指针
开发语言·后端·golang
Lenyiin4 小时前
《LeetCode 热题 100》整整 100 题量大管饱题解套餐 中
java·c++·python·leetcode·面试·刷题·lenyiin
蒟蒻小袁4 小时前
力扣面试150题--颠倒二进制位
java·算法·leetcode
4 小时前
LeetCode Hot 100 括号生成
算法·leetcode·职场和发展
逝雪Yuki4 小时前
Leetcode——42. 接雨水
c++·算法·leetcode·双指针·接雨水
姜不吃葱5 小时前
【力扣热题100】哈希——两数之和
算法·leetcode·哈希算法·力扣热题100
一匹电信狗6 小时前
【C++】手搓一个STL风格的vector容器
c语言·数据结构·c++·算法·leetcode·stl·visual studio
逝雪Yuki7 小时前
Leetcode——11. 盛最多水的容器
c++·算法·leetcode·双指针
岁忧16 小时前
macOS配置 GO语言环境
开发语言·macos·golang
薰衣草233318 小时前
一天两道力扣(6)
算法·leetcode