mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #2709 from suinming/suinming
feat: 動態規劃leetcode#714(買賣股票最佳時機含手續費),新增python解法
This commit is contained in:
@ -188,6 +188,20 @@ class Solution:
|
|||||||
return max(dp[-1][0], dp[-1][1])
|
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:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user