<filename>Algorithms/0714_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee/Python/Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee_Solution_1.py

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

class Solution:
    def maxProfit(self, prices, fee):
        length = len(prices)
        if length <= 1: return 0

        cash, profit = -prices[0], 0

        for i in range(1, length):
            profit = max(profit, profit + prices[i] - fee)
            cash = max(cash, profit - prices[i])

        return profit





<filename>Algorithms/0714_Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee/Python/Best_Time_to_Buy_and_Sell_Stock_with_Transaction_Fee_Solution_1.py

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

class Solution:
    def maxProfit(self, prices, fee):
        length = len(prices)
        if length <= 1: return 0

        cash, profit = -prices[0], 0

        for i in range(1, length):
            profit = max(profit, profit + prices[i] - fee)
            cash = max(cash, profit - prices[i])

        return profit





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