Skip to content
LC-0714 Medium LeetCode

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

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 70% Topics: Array, Dynamic Programming, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# 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

Solution from kamyu104/LeetCode-Solutions · MIT