Merge pull request #2709 from suinming/suinming

feat: 動態規劃leetcode#714(買賣股票最佳時機含手續費),新增python解法
This commit is contained in:
程序员Carl
2024-09-03 10:30:18 +08:00
committed by GitHub

View File

@ -188,6 +188,20 @@ class Solution:
return max(dp[-1][0], dp[-1][1])
```
```python
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
# 持有股票手上的最大現金
hold = -prices[0] - fee
# 不持有股票手上的最大現金
not_hold = 0
for price in prices[1:]:
new_hold = max(hold, not_hold - price - fee)
new_not_hold = max(not_hold, hold + price)
hold, not_hold = new_hold, new_not_hold
return not_hold
```
### Go
```go