Skip to content

Instantly share code, notes, and snippets.

@edenthecat
Created December 9, 2015 19:39
Show Gist options
  • Save edenthecat/0fe55c5523ab2e46aeaa to your computer and use it in GitHub Desktop.
Save edenthecat/0fe55c5523ab2e46aeaa to your computer and use it in GitHub Desktop.
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])
@edenthecat
Copy link
Author

For some reason it's not showing negative results for when the prices go down all day..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment