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

This commit is contained in:
Baturu
2021-06-07 21:56:50 -07:00
committed by GitHub
parent a4b7399acb
commit 7036c5d1b2

View File

@ -139,7 +139,17 @@ public int maxProfit(int[] prices, int fee) {
``` ```
Python 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 Go