Last active
April 26, 2019 12:18
-
-
Save LucasBarbara/f9c1b1d73f3d148715f014f131d711ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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