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 0ed9d11

Browse files
🐱(dynamic): 714. 买卖股票的最佳时机含手续费
1 parent 1d596ae commit 0ed9d11

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

‎docs/algorithm/dynamic/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,31 @@ class Solution(object):
698698
List1 = List1+List2
699699

700700
return List1[:num+1]
701+
```
702+
703+
## 714. 买卖股票的最佳时机含手续费
704+
705+
[原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
706+
707+
### 思路
708+
709+
```python
710+
class Solution(object):
711+
def maxProfit(self, prices, fee):
712+
"""
713+
:type prices: List[int]
714+
:type fee: int
715+
:rtype: int
716+
"""
717+
# cash:不持有
718+
cash = 0
719+
# hold:持有
720+
hold = -prices[0]
721+
for i in range(1, len(prices)):
722+
p = prices[i]
723+
# 选择卖出或不变
724+
cash = max(cash, hold + prices[i] - fee)
725+
# 选择买入或不变
726+
hold = max(hold, cash - prices[i])
727+
return cash
701728
```

‎docs/algorithm/greedy/README.md‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -479,33 +479,6 @@ class Solution:
479479
return "Dire" if len(rq) == 0 else "Radiant"
480480
```
481481

482-
## 714. 买卖股票的最佳时机含手续费
483-
484-
[原题链接](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)
485-
486-
### 思路
487-
488-
```python
489-
class Solution(object):
490-
def maxProfit(self, prices, fee):
491-
"""
492-
:type prices: List[int]
493-
:type fee: int
494-
:rtype: int
495-
"""
496-
# cash:不持有
497-
cash = 0
498-
# hold:持有
499-
hold = -prices[0]
500-
for i in range(1, len(prices)):
501-
p = prices[i]
502-
# 选择卖出或不变
503-
cash = max(cash, hold + prices[i] - fee)
504-
# 选择买入或不变
505-
hold = max(hold, cash - prices[i])
506-
return cash
507-
```
508-
509482
## 955. 删列造序 II
510483

511484
[原题链接](https://leetcode-cn.com/problems/delete-columns-to-make-sorted-ii/)

0 commit comments

Comments
(0)

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