mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0714.买卖股票的最佳时机含手续费(动态规划).md
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user