Skip to content

Instantly share code, notes, and snippets.

@LucasBarbara
Last active April 26, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucasBarbara/f9c1b1d73f3d148715f014f131d711ec to your computer and use it in GitHub Desktop.
Save LucasBarbara/f9c1b1d73f3d148715f014f131d711ec to your computer and use it in GitHub Desktop.
def stock_picker(arr)
max_value = arr[1]
min_value = arr[0]
best_day_to_buy = 0
best_day_to_sell = 0
arr.each_with_index do |v, i|
if max_value < v && i > 0
max_value = v
best_day_to_sell = i
end
end
arr.each_with_index do |v, i|
if min_value > v && i < best_day_to_sell
min_value = v
best_day_to_buy = i
end
end
p [best_day_to_buy, best_day_to_sell]
puts "For a profit of: $#{max_value - min_value}"
end
stock_picker([18, 3, 6, 9, 15, 18, 6, 9, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment