714. 买卖股票的最佳时机含手续费 - 力扣(LeetCode)
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
if not prices:
return 0
# 初始状态
hold, cash = -prices[0], 0
for price in prices[1:]:
hold = max(hold, cash - price) # 继续持有 or 买入新的股票
cash = max(cash, hold + price - fee) # 继续不持有 or 卖出股票
return cash