<jupyter_start><jupyter_text>问题描述：    您将得到一个整数数组prices，其中i-th元素是给定股票在当天的价格i；    非负整数fee代表交易费用。    您可以根据需要完成任意数量的交易，但是您需要为每笔交易支付交易费。    您一次只能购买不超过1股的股票（即必须先出售股票才能再次购买。）    求：返回您可以赚取的最大利润。例题1：    Input: prices = [1, 3, 2, 8, 4, 9], fee = 2    Output: 8    Explanation: The maximum profit can be achieved by:    Buying at prices[0] = 1    Selling at prices[3] = 8    Buying at prices[4] = 4    Selling at prices[5] = 9    The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.<jupyter_code>class Solution:
    def maxProfit(self, prices, fee: int) -> int:
        # 动态规划的方法
        hold = -float('inf')/2
        sold = 0
        for price in prices:
            print(hold, sold, price, 'before')
            # 选择买进或者不买进
            hold = max(hold, sold - price) # sold, 是现在手中的前， sold-price=剩余的钱
            # 选择卖出或不卖出
            sold = max(sold, hold + price - fee)
            print(hold, sold, price, 'after')
        return sold
prices_ = [100, 3, 2, 8, 4, 9]
fee_ = 2
solution = Solution()
solution.maxProfit(prices_, fee_)<jupyter_output>-inf 0 100 before
-100 0 100 after
-100 0 3 before
-3 0 3 after
-3 0 2 before
-2 0 2 after
-2 0 8 before
-2 4 8 after
-2 4 4 before
0 4 4 after
0 4 9 before
0 7 9 after<jupyter_text>动态规划的特点，每个值都有选或不选两个选择<jupyter_code>class Solution:
    def maxProfit(self, prices, fee: int) -> int:
        # 动态规划的方法，对于每一天，都可以选则买进、或者卖出
        pass
prices_ = [100, 3, 2, 8, 4, 9]
fee_ = 2
solution = Solution()
solution.maxProfit(prices_, fee_)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup