Skip to content

Instantly share code, notes, and snippets.

@amardaxini
Created August 22, 2013 13:03
Show Gist options
  • Save amardaxini/6306838 to your computer and use it in GitHub Desktop.
Save amardaxini/6306838 to your computer and use it in GitHub Desktop.
Stock Options
79 14 3 21 104 54 12 9 94 1 96 103
require 'rubygems'
require 'pry'
a =[79, 14, 3, 21, 104, 54, 12, 9, 94 , 1 , 96, 103]
max_profit =0
max_value = a[a.size-1]
max_index= a.size - 1
sell_index = a.size - 1
buy_index = a.size - 1
index= a.size - 1
array_index = index
while index >1 do
if(max_value < a[index].to_f)
max_value = a[index]
max_index = index
end
if(a[max_index] - a[index] > max_profit )
sell_index= max_index
buy_index = index
max_profit = a[max_index]-a[index]
end
index= index - 1
end
puts "Buy at #{buy_index} value #{a[buy_index]}"
puts "sell at #{sell_index} value #{a[sell_index]} "
puts "Profit = #{max_profit}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment