mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
增加买卖股票最佳时机Ⅱ,买卖股票最佳时机Ⅲ,买卖股票最佳时机Ⅳ go版本
This commit is contained in:
@@ -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:
|
||||
|
||||
> 版本一:
|
||||
|
||||
Reference in New Issue
Block a user