Reconstruct Itinerary
Explore how to solve the Reconstruct Itinerary problem by modeling tickets as a graph and using a depth-first search approach. Understand creating mappings of airports, performing post-order DFS, and ensuring the lexical order of the itinerary. This lesson helps you prepare for coding interviews with practical algorithm techniques using Ruby.
We'll cover the following...
Description
Assume we have an array of airline tickets represented as pairs of departure and arrival airports like [from, to]. We have to reconstruct the itinerary in order. All tickets belong to a man who departs from a specified location, e.g., JFK. Therefore, the itinerary must begin with JFK. There might be multiple valid itineraries; you must return the itinerary with the smallest lexical order.
Constraints
Consider the following constraints:
- 1 <=
tickets.length<= 300 tickets[i].lengthmust be 2.- , == 3
- and