Created
February 8, 2016 22:26
-
-
Save jacek213/24268b5a457db12d3f8b 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 solution(a) | |
@top_gain = 0 | |
a.each_with_index do |price, day| | |
if day == 0 | |
@bottom = price | |
@top = price | |
end | |
if price > @top | |
@top = price | |
end | |
if price < @bottom | |
@bottom = price | |
end | |
possible_gain = price - @bottom | |
if possible_gain > @top_gain | |
@top_gain = possible_gain | |
else | |
if price < @bottom | |
@bottom = price | |
end | |
end | |
end | |
@top_gain | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment