Skip to content

Instantly share code, notes, and snippets.

@ready4god2513
Forked from ryanswood/gist:752db6a3a3f2e3b48ede
Last active August 29, 2015 14:03
Show Gist options
  • Save ready4god2513/21576287ab543a50b721 to your computer and use it in GitHub Desktop.
Save ready4god2513/21576287ab543a50b721 to your computer and use it in GitHub Desktop.
# Given an array_of_ints, find the highest_product you can get from three of the integers.
# The input array_of_ints will always have at least three integers.
def highest_product(arr)
highest = arr[0]
lowest = arr[0]
# This just set the absolute baseline to work with.
# Grab the first (n) elements and multiply them. Then as we walk through continue to look for
# higher numbers
highest_of_two = lowest_of_two = arr[0] * arr[1]
highest_of_three = highest_of_two * arr[2]
arr.each do |current|
highest_of_three = [
highest_of_three,
(highest_of_two * current),
(lowest_of_two * current)
].max
highest_of_two = [
highest_of_two,
current * highest,
current * lowest
].max
lowest_of_two = [
lowest_of_two,
current * highest,
current * lowest
].min
highest = [highest, current].max
lowest = [lowest, current].min
puts "current = #{current} & highest = #{highest} & highest_of_two = #{highest_of_two} & highest_of_three = #{highest_of_three}"
end
highest_of_three
end
def assert_equal(actual, expected)
puts "Expected: #{expected} and got #{actual}" unless actual == expected
end
assert_equal(highest_product([1,4,5,3,6]), 120)
assert_equal(highest_product([1,10,1,-5,1,-100]), 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment