mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
Merge branch 'master' into master
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:
|
||||
|
||||
> 版本一:
|
||||
@@ -355,4 +387,4 @@ func max(a, b int) int {
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
||||
Reference in New Issue
Block a user