Merge pull request #144 from QuinnDK/添加0121买卖股票的最佳时机Go版本

添加0121买卖股票的最佳时机Go版本
This commit is contained in:
Carl Sun
2021-05-16 13:33:20 +08:00
committed by GitHub

View File

@ -219,6 +219,31 @@ Python
Go
```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
}
```