增加买卖股票最佳时机Ⅱ,买卖股票最佳时机Ⅲ,买卖股票最佳时机Ⅳ go版本

This commit is contained in:
baici1
2021-10-22 16:58:02 +08:00
parent 99d28fcc00
commit 35154b7be3
3 changed files with 93 additions and 0 deletions

View File

@@ -278,6 +278,38 @@ class Solution:
return dp[4]
```
Go:
```go
func maxProfit(prices []int) int {
dp:=make([][]int,len(prices))
for i:=0;i<len(prices);i++{
dp[i]=make([]int,5)
}
dp[0][0]=0
dp[0][1]=-prices[0]
dp[0][2]=0
dp[0][3]=-prices[0]
dp[0][4]=0
for i:=1;i<len(prices);i++{
dp[i][0]=dp[i-1][0]
dp[i][1]=max(dp[i-1][1],dp[i-1][0]-prices[i])
dp[i][2]=max(dp[i-1][2],dp[i-1][1]+prices[i])
dp[i][3]=max(dp[i-1][3],dp[i-1][2]-prices[i])
dp[i][4]=max(dp[i-1][4],dp[i-1][3]+prices[i])
}
return dp[len(prices)-1][4]
}
func max(a,b int)int{
if a>b{
return a
}
return b
}
```
JavaScript
> 版本一: