Skip to content

Instantly share code, notes, and snippets.

@jacek213
Created February 8, 2016 22:26
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 jacek213/24268b5a457db12d3f8b to your computer and use it in GitHub Desktop.
Save jacek213/24268b5a457db12d3f8b to your computer and use it in GitHub Desktop.
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