You get a vector tickets. Each ticket is one flight, written as a pair [from, to] of airport codes.
Return the airports of a trip that uses every ticket exactly once, in the order you visit them.
The trip starts at your home airport, "HOM". Each flight leaves from the airport where the last one landed. You may pass through an airport many times.
Often several trips use all the tickets. Return the one that comes first in alphabetical order. To compare two trips, read them airport by airport from the start. The first airport where they differ decides.
Such a trip always exists.
Example 1:
Input: tickets = [["ROM","OSL"],["HOM","PAR"],["PAR","ROM"]] Output: ["HOM","PAR","ROM","OSL"] Explanation: Only one ticket leaves each airport. So the only trip is HOM → PAR → ROM → OSL.
Example 2:
Input: tickets = [["HOM","ZRH"],["BER","HOM"],["HOM","BER"],["ZRH","HOM"]] Output: ["HOM","BER","HOM","ZRH","HOM"] Explanation: Two trips use all four tickets: HOM → BER → HOM → ZRH → HOM HOM → ZRH → HOM → BER → HOM They differ at the second airport. BER comes before ZRH.
Example 3:
Input: tickets = [["HOM","VIE"],["HOM","ATH"],["VIE","HOM"]] Output: ["HOM","VIE","HOM","ATH"] Explanation: ATH comes before VIE. But no ticket leaves ATH, so a trip that flies there first ends there. The tickets to VIE and back would stay unused. So the trip goes to VIE first.
Constraints:
- 1 to 300 tickets.
- Each airport code is 3 capital letters.
- A ticket never goes from an airport to itself.
- The same ticket can appear more than once.
Note:
Your solution should run in O(E log E) time, for E tickets.
Free plan: 30 tutor messages. Premium is unlimited.
