Skip to content

Instantly share code, notes, and snippets.

@Hamled
Created December 11, 2015 02:06
Show Gist options
  • Save Hamled/d6f1defdbe50962b068b to your computer and use it in GitHub Desktop.
Save Hamled/d6f1defdbe50962b068b to your computer and use it in GitHub Desktop.
def find_movies(flight_length, movie_lengths)
movie_complements = Set.new
movie_lengths.each do |movie_length|
# We have a good movie pair if this length is the complement of a previous movie's length
return true if movie_complements.include? movie_length
# Otherwise, track the complement of this movie's length for checking against succeeding movie lengths
movie_complements << (flight_length - movie_length)
end
# We didn't find any movie pairs
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment