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 
}
相关推荐
还不秃顶的计科生8 分钟前
力扣第84题:完全平方数
算法·leetcode·职场和发展
呆萌很19 分钟前
【GO】switch 练习题
golang
sheeta199821 分钟前
LeetCode 每日一题笔记 日期:2025.03.22 题目:1886.判断矩阵经轮转后是否一致
笔记·leetcode·矩阵
米粒125 分钟前
力扣算法刷题 Day 20
算法·leetcode·职场和发展
不想看见40434 分钟前
Min Stack栈和队列--力扣101算法题解笔记
java·笔记·leetcode
北顾笙98035 分钟前
测开准备-day04数据结构力扣
数据结构·算法·leetcode
阿里嘎多哈基米1 小时前
速通Hot100-Day10——二叉树
算法·leetcode·二叉树·递归·平衡二叉树
窝子面1 小时前
LeetCode练题六:dfs与bfs
leetcode·深度优先·宽度优先
hanlin031 小时前
刷题笔记:力扣第6题-Z字形变换
笔记·算法·leetcode
郝学胜-神的一滴3 小时前
Leetcode 969 煎饼排序✨:翻转间的数组排序艺术
数据结构·c++·算法·leetcode·面试