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

State Machine

class Solution: 
    def maxProfit(self, prices, fee): 
        hold = -prices[0] 
        unhold = 0 
         
        for price in prices: 
            hold_old = hold 
            hold = max(hold, unhold - price) 
            unhold = max(hold_old + price - fee, unhold) 
             
        return unhold

Simulation

class Solution:
    def maxProfit(self, prices, fee):
        n = len(prices)
        if n < 2:
             return 0
        ans = 0
        minimum = prices[0]
        for i in range(1, n):
            if prices[i] < minimum:
                minimum = prices[i]

            elif prices[i] > minimum + fee:
                ans += prices[i] - fee - minimum
                minimum = prices[i] - fee

        return ans