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 
}
相关推荐
CoderYanger41 分钟前
A.每日一题:856. 括号的分数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
tachibana21 小时前
Golang 中数组和切片的区别?
开发语言·后端·golang·数组·切片
All for pursuit.1 小时前
【动态规划-8】152.乘积最大子数组
数据结构·c++·算法·leetcode·动态规划
小小龙学IT2 小时前
Redis 源码深度解析:从常用命令到内部实现
redis·golang·开源
Nil2082 小时前
leetcode 152乘积最大子数组
leetcode
夏幻灵13 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG15 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z15 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
圣保罗的大教堂16 小时前
leetcode 301. 删除无效的括号 困难
leetcode
北冥you鱼17 小时前
Go 语言空接口(interface{})使用场景与最佳实践
开发语言·windows·golang