Advertisement

Reconstruct Itinerary - LeetCode 332 Solution

Reconstruct Itinerary - Complete Solution Guide

Reconstruct Itinerary is LeetCode problem 332, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a list of airline tickets where tickets[i] = [from i , to i ] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it. All of the tickets belong to a man who departs from "JFK" , thus, the itinerary must begin with "JFK" . If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical ord

Detailed Explanation

The problem requires reconstructing a flight itinerary from a given list of tickets. Each ticket represents a flight from one airport to another. The itinerary must start from "JFK", and all tickets must be used exactly once. If multiple valid itineraries exist, the itinerary with the smallest lexical order should be returned. It is guaranteed that at least one valid itinerary exists.

Solution Approach

The provided solution uses a Depth-First Search (DFS) algorithm to traverse the graph of airports and tickets. The algorithm builds an adjacency list representation of the graph where keys are departure airports and values are a list of possible destination airports. The destinations are sorted in reverse lexicographical order. The DFS starts from "JFK", recursively visits unvisited airports based on the ticket information and adds the airports to the itinerary in reverse order of visiting. Finally, the itinerary is reversed to obtain the correct order.

Step-by-Step Algorithm

  1. Step 1: Create a graph (adjacency list) to represent the tickets. The keys are the departure airports, and the values are lists of possible destination airports.
  2. Step 2: Sort the destination airports in reverse lexicographical order for each departure airport. This ensures that when multiple options are available, the algorithm chooses the smallest lexical order airport first.
  3. Step 3: Initialize an empty itinerary list.
  4. Step 4: Implement a recursive DFS function that takes the current airport as input.
  5. Step 5: Inside the DFS function, while there are unvisited destinations from the current airport, pop the last destination airport from the sorted list (due to reverse sorting, popping the last element gives the smallest lexical element). Recursively call DFS on the popped destination airport.
  6. Step 6: After visiting all reachable airports from the current airport, append the current airport to the itinerary list.
  7. Step 7: Start the DFS from "JFK".
  8. Step 8: Reverse the itinerary list to obtain the correct order, as the DFS adds airports in reverse order of traversal.
  9. Step 9: Return the reversed itinerary.

Key Insights

  • Insight 1: The problem can be modeled as finding an Eulerian path/circuit in a directed graph, where airports are nodes and tickets are edges.
  • Insight 2: The lexical order constraint requires sorting the destination airports in reverse lexicographical order before traversing the graph using Depth-First Search (DFS). This ensures the algorithm explores the smallest lexical order paths first.
  • Insight 3: Using a data structure that allows efficient removal of edges (tickets) is crucial for performance. In Python, using `pop()` on a sorted list allows us to remove destinations in the desired order.

Complexity Analysis

Time Complexity: O(E)

Space Complexity: O(E)

Topics

This problem involves: Depth-First Search, Graph, Eulerian Circuit.

Companies

Asked at: Booking.com, Flipkart, Netflix, Pinterest, Snap, Twilio, Yandex, eBay.