Skip to content

Instantly share code, notes, and snippets.

@tonyvince
Last active May 28, 2020 09:15
Show Gist options
  • Save tonyvince/7abe1f00b12d73458f64dd679cf1c62a to your computer and use it in GitHub Desktop.
Save tonyvince/7abe1f00b12d73458f64dd679cf1c62a to your computer and use it in GitHub Desktop.
# Trip Optimization (maximize your layover)
# You have a [X]-hour layover in Paris and want to book some Trips. You prefer to book as few Trips as possible to enjoy the last
# [X] hours with minimum context switch. How will you arrange your time? Assuming that there is no time cost for transportation
# between Trips.
# Input
# An array of Trips durations (hours). e.g. [1, 3, 6]
# Total remaining hours. e.g. 15
# Output:
# Minimum number of purchases. e.g. 3 (6 + 6 + 3 = 15)
# If there is no way to fill total remaining hours, return -1.
# Imput Sample:
# [1 3 6]
# Output
# 3
def minPurchases(trip_durations, total_hours)
trips = []
trip_durations.sort.reverse.each do |trip|
loop do
break if trips.sum + trip > total_hours
trips << trip
end
end
return -1 if trips.size.zero?
trips.count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment