title :
Given A list of airline tickets represented by pairs of departure and arrival airports [from, to]
, reconstruct the itinerary in Order. All of the tickets belong to a man departs from JFK
. Thus, the itinerary must begin with JFK
.
Note:
- If There is multiple valid itineraries, you should return the itinerary that have the smallest lexical order when read as A single string. For example, the itinerary have
["JFK", "LGA"]
a smaller lexical order than ["JFK", "LGB"]
.
- All airports is represented by three capital letters (IATA code).
- Assume all tickets form at least one valid itinerary.
Example 1:
tickets
=[["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"]
.
Example 2:
tickets
=[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"]
.
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]
. But it's larger in lexical order.
test instructions and analysis: Save all the tickets in an array, and finally we are going to find a path to run out of tickets, and if there are multiple paths, find the smallest dictionary order. The first way to think of it is to save each point and its neighbors with a hashmap<string,priorityqueue<string>>, and then use a deep traversal, and the result is the answer (because the smallest path is used each time). Another way is to find the end point each time, and then delete the points to continue to find the next end point, and finally the result reversal can be.
in fact, the problem is to calculate the smallest Euler path. Through analysis you can know:If there is a forward graph, there is a Euler path (not a Euclidean loop), then the point in the graph can have up to two points: degree (into)!=degree (out), and these two points, one into degrees > out, one in degrees < out; also possible all points degree (into) = =degree (out), there is a Euler circuitObviously, since the title guarantees the existence of Euler's path, then JFK is the point of entry < exit, and there is a point in > out, or all of the points ingreed: Starting with JFK, each time you pick the smallest path, if you can't go down, you may encounter the end Point PP (the point of the penetration > exit), thus forming the main path from JFK to pp. The left side is only possible to form a ring, as long as the ring is incorporated into the main path to complete the minimum Euler path search!!
Code:
Public classSolution {Map<string, priorityqueue<string>> map=NewHashmap<>(); List<String> res =NewArraylist<>(); PublicList<string>finditinerary (string[][] tickets) { for(string[] ticket:tickets) {map.computeifabsent (ticket[0],k->NewPriorityqueue<> ()). Add (ticket[1]); } DFS ("JFK"); Collections.reverse (RES); returnRes; } Public voidDFS (String node) {priorityqueue<String> Priorityqueue =map.get (node); while(priorityqueue!=NULL&&!priorityqueue.isempty ()) DFS (Priorityqueue.poll ()); Res.add (node); }}
[Leetcode] 332. Reconstruct itinerary Java