每遍历到一个价格:
- 如果可以赚,就卖掉,并存入账户;如果不赚,就假装前一次没有买入
- 买入
class Solution: def maxProfit(self , prices ): # write code here c = 0 last_price = prices[0] for i in prices: print(last_price, c) if i > last_price: #如果可以赚,就卖掉存入账户;如果不赚,就假装前一次没有买入 c += i - last_price #买入 last_price = i return c