Leetcode 714 Solution

This article provides solution to leetcode question 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

Solution

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        cash, hold = 0, -prices[0]

        ans = 0
        for i in range(1, len(prices)):
            cash = max(cash, hold + prices[i] - fee)
            hold = max(hold, cash - prices[i])
            ans = max(ans, cash, hold)

        return ans