Medium

Best Time to Buy and Sell Stock with Transaction FeePython

Full explanation · Time O(n) · Space O(1)

# Time:  O(n)
# Space: O(1)


class Solution(object):
    def maxProfit(self, prices, fee):
        """
        :type prices: List[int]
        :type fee: int
        :rtype: int
        """
        cash, hold = 0, -prices[0]
        for i in xrange(1, len(prices)):
            cash = max(cash, hold+prices[i]-fee)
            hold = max(hold, cash-prices[i])
        return cash