Created
August 8, 2017 21:41
-
-
Save mecampbellsoup/b823f1926361f83dfaeb4ab3b10ed570 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
require 'pry' | |
def maxDifference(a) | |
min = 0 | |
max_diff = 0 | |
a.each_with_index do |value, index| | |
if value < a[min] | |
min = index | |
end | |
diff = value - a[min] | |
if diff > max_diff | |
max_diff = diff | |
end | |
end | |
# If max_diff is 0 then we had a strictly decreasing-in-size list of elements. | |
max_diff == 0 ? -1 : max_diff | |
end | |
require 'minitest/autorun' | |
class MaxDifferenceTest < Minitest::Test | |
def test_max_difference_is_positive | |
diff = maxDifference([2, 3, 10, 2, 4, 8, 1]) | |
assert diff == 8 | |
end | |
def test_no_max_difference | |
diff = maxDifference([5, 4, 3, 2, 1]) | |
assert diff == -1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment