-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchItinerary.rb
38 lines (32 loc) · 934 Bytes
/
fetchItinerary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def fetch_itinerary(flights, start)
solve(flights, start, [])
end
def solve(flights, start, current_itinerary)
if flights.size <= 0 || !flights
return current_itinerary + [start]
end
update_itinerary = nil
flights.each.with_index do |(origin, destination), index|
if start == origin
new_itinerary = current_itinerary + [origin]
child_itinerary = solve(
remaining_flights(flights, index), destination, new_itinerary
)
if !update_itinerary || child_itinerary.size > update_itinerary.size
update_itinerary = child_itinerary
end
end
end
update_itinerary
end
def remaining_flights(flights, index)
if index <= 0
return flights[(index + 1).. -1]
else
return flights[0..(index - 1)] + flights[(index + 1)..-1]
end
end
# fetch_itinerary([
# ['SFO', 'HKO'], ['YYZ', 'SFO'], ['YUL', 'YYZ'],
# ['HKO', 'ORD']
# ], "YUL") # => ["YUL", "YYZ, "SFO", "HKO"]