Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Created November 6, 2014 17:23
Show Gist options
  • Save imouaddine/777c881d689699345502 to your computer and use it in GitHub Desktop.
Save imouaddine/777c881d689699345502 to your computer and use it in GitHub Desktop.
def product_max(a)
max_so_far = 1
min_so_far = 1
max = 1
a.each do |i|
if i == 0
max_so_far = 1
min_so_far = 1
elsif i > 0
max_so_far = max_so_far * i
min_so_far = [min_so_far * i, 1].min
else
max_so_far = [min_so_far * i, 1].max
min_so_far = max_so_far * i
end
if max_so_far > max
max = max_so_far
end
end
max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment