Ruby O(n) solution


  • 0
    S
    def max_profit(prices)
        result = 0
        buy_price = prices[0]
        prices.each do |price|
            buy_price = price if price < buy_price
            result = [result, price - buy_price].max
        end
        result
    end
    

Log in to reply
 

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