Skip to content

Instantly share code, notes, and snippets.

@edenthecat
Created December 9, 2015 19:39

Revisions

  1. edenthecat created this gist Dec 9, 2015.
    22 changes: 22 additions & 0 deletions getMaxProfit.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    def getMaxProfit(stock_prices_yesterday)
    if stock_prices_yesterday.index(stock_prices_yesterday.min) < stock_prices_yesterday.index(stock_prices_yesterday.max)
    max_profit = (stock_prices_yesterday.max - stock_prices_yesterday.min)
    else
    possible_profits = []

    minIndex = 0
    maxIndex = stock_prices_yesterday.length

    stock_prices_yesterday[0...(maxIndex-1)].each do |i|
    stock_prices_yesterday[minIndex...maxIndex].each do |j|
    possible_profits.push(j - i)
    end
    minIndex += 1
    end
    max_profit = possible_profits.max
    end

    puts "max profit: " + max_profit.to_s
    end

    getMaxProfit([11, 10, 9, 8, 7])