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