Description
Given a list of airline tickets as pairs [from, to], reconstruct the itinerary in order. The itinerary must begin with 'JFK'. If multiple valid itineraries exist, return the one with the smallest lexical order.
Examples
Input:
tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]Output:
["JFK","MUC","LHR","SFO","SJC"]Explanation:
Valid path starting from JFK.
Input:
tickets = [[1]]Output:
[1]Explanation:
Minimal case with a single-element matrix.
Input:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]Output:
["JFK","ATL","JFK","SFO","ATL","SFO"]Explanation:
This example demonstrates handling multiple tickets from the same departure city. From JFK, destinations ATL and SFO are available. Since ATL < SFO lexicographically, ATL is chosen first, following the greedy approach of picking the smallest destination at each step.
Constraints
- •
1 ≤ tickets.length ≤ 300 - •
tickets[i].length == 2