Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cf8f9e8

Browse files
Merge pull request youngyangyang04#2709 from suinming/suinming
feat: 動態規劃leetcode#714(買賣股票最佳時機含手續費),新增python解法
2 parents bdb0000 + 051459a commit cf8f9e8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ class Solution:
188188
return max(dp[-1][0], dp[-1][1])
189189
```
190190

191+
```python
192+
class Solution:
193+
def maxProfit(self, prices: List[int], fee: int) -> int:
194+
# 持有股票手上的最大現金
195+
hold = -prices[0] - fee
196+
# 不持有股票手上的最大現金
197+
not_hold = 0
198+
for price in prices[1:]:
199+
new_hold = max(hold, not_hold - price - fee)
200+
new_not_hold = max(not_hold, hold + price)
201+
hold, not_hold = new_hold, new_not_hold
202+
return not_hold
203+
```
204+
191205
### Go:
192206

193207
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /