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 1d596ae

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

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎docs/algorithm/greedy/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,33 @@ 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+
482509
## 955. 删列造序 II
483510

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

0 commit comments

Comments
(0)

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