Skip to content

Instantly share code, notes, and snippets.

@MBM1607
Created February 25, 2021 08:08
Show Gist options
  • Save MBM1607/a5e10b9d03983d8474f6870045597d8b to your computer and use it in GitHub Desktop.
Save MBM1607/a5e10b9d03983d8474f6870045597d8b to your computer and use it in GitHub Desktop.
Ruby Stock Picker
def stock_picker(stocks)
profits = {}
stocks.each_with_index do |price_i, i|
stocks.each_with_index do |price_j, j|
profits[[i, j]] = price_j - price_i if j > i
end
end
index, _ = profits.max_by { |k, v| v }
return index
end
p stock_picker([17,3,6,9,15,8,6,1,10])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment