ruby solution


  • 0
    H

    @param {Integer[]} prices

    @return {Integer}

    def max_profit(prices)
    if prices.length == 1
    return 0
    end

    buy = prices[0]
    profit = 0
    
    for i in 1...prices.length do
        if profit < prices[i] - buy
            profit = prices[i] - buy
        end
        
        if buy > prices[i]
            buy = prices[i]
        end
    end
    
    return profit
    

    end


  • 0
    H
        if prices.length == 1
            return 0
        end
        
        buy = prices[0]
        profit = 0
        
        for i in 1...prices.length do
            if profit < prices[i] - buy
                profit = prices[i] - buy
            end
            
            if buy > prices[i]
                buy = prices[i]
            end
        end
        
        return profit
    end```

Log in to reply
 

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