Merge pull request #358 from z80160280/z80160280-patch-7

Update 0714.买卖股票的最佳时机含手续费(动态规划).md
This commit is contained in:
程序员Carl
2021-06-10 10:44:41 +08:00
committed by GitHub

View File

@ -139,7 +139,17 @@ public int maxProfit(int[] prices, int fee) {
```
Python
```python
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
n = len(prices)
dp = [[0] * 2 for _ in range(n)]
dp[0][0] = -prices[0] #持股票
for i in range(1, n):
dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i])
dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i] - fee)
return max(dp[-1][0], dp[-1][1])
```
Go