feat: 動態規劃leetcode#714,新增python解法

This commit is contained in:
suinming
2024-08-27 10:45:45 +08:00
parent eba858b4e5
commit 051459a542

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