714. Best Time to Buy and Sell Stock with Transaction Fee

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/

这道题是 122 Best time to buy and sell || 的变形,可交易无数次,但有transaction fee

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        buy = -prices[0]
        sell = 0
        for price in prices[1:]:
            prevBuy = buy
            buy = max(buy,sell - price)
            sell = max(sell, prevBuy + price - fee)
        return sell
        

Last updated

Was this helpful?