Ruby Time O(n), Space O(1)


  • 0
    S
    def max_profit(prices, fee)
      return 0 if prices.empty?
      max, buy, sell = 0, - Float::INFINITY, 0
      for pp in prices
        buy  = [ buy, sell - pp ].max
        sell = [ sell, pp + buy - fee ].max
        max = [ max, sell ].max
      end
      max
    end
    

    Simple and works.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.