mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #144 from QuinnDK/添加0121买卖股票的最佳时机Go版本
添加0121买卖股票的最佳时机Go版本
This commit is contained in:
@ -219,6 +219,31 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user