Update 0714.买卖股票的最佳时机含手续费(动态规划).md

This commit is contained in:
YusenAi
2021-09-01 16:39:46 +08:00
committed by GitHub
parent a66e1801c3
commit 4926d659a7

View File

@ -152,6 +152,24 @@ class Solution:
```
Go
```Go
func maxProfit(prices []int, fee int) int {
n := len(prices)
dp := make([][2]int, n)
dp[0][0] = -prices[0]
for i := 1; i < n; i++ {
dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]-fee)
dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i])
}
return dp[n-1][1]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
```
Javascript
```javascript