Reconstruct Itinerary(Medium)
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 who
departs from JFK. Thus, the itinerary must begin with JFK.
Note:
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 order than ["JFK", "LGB"].
All airports are represented by three capital letters (IATA code).
You may 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 is larger in lexical order.
解题思路1:欧拉通路(Eulerian path)
将机场视为顶点,机票视为看做有向边,可以构成一个有向图。
通过图(无向图或有向图)中所有边且每边仅通过一次的通路称为欧拉通路,相应的回路称为欧拉回路。具有欧拉回路的图称为欧拉图(Euler Graph),具有欧拉通路而无欧拉回路的图称为半欧拉图。
因此题目的实质就是从JFK顶点出发寻找欧拉通路,可以利用Hierholzer算法。
public List<String> findItinerary(String[][] tickets) {
Map<String, PriorityQueue<String>> targets = new HashMap<>();
for (String[] ticket : tickets)
targets.computeIfAbsent(ticket[0], k -> new PriorityQueue()).add(ticket[1]);
List<String> route = new LinkedList();
Stack<String> stack = new Stack<>();
stack.push("JFK");
while (!stack.empty()) {
String airport = stack.peek();
while (targets.containsKey(airport) && !targets.get(airport).isEmpty()){
stack.push( targets.get(airport).poll() );
airport = stack.peek();
}
route.add(0, stack.pop());
}
return route;
}
public static void main(String[] args) {
Test instance = new Test();
//Return ["JFK","ATL","JFK","SFO","ATL","SFO"]
String[][] tickets = {{"JFK","SFO"},{"JFK","ATL"},{"SFO","ATL"},{"ATL","JFK"},{"ATL","SFO"}};
int[] nums = {4,1,1,2,2};
System.out.println("doing... " + instance.findItinerary(tickets));
}
解题思路2:DFS+Backtracking
public List<String> findItinerary(String[][] tickets) {
ArrayList<String> result = new ArrayList<String>();
if(tickets == null || tickets.length == 0){
return result;
}
int total = tickets.length + 1;
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for(int i = 0; i < tickets.length; i++){
if(map.containsKey(tickets[i][0])){
ArrayList<String> tmp = map.get(tickets[i][0]);
listAdd(tickets[i][1], tmp);
} else{
ArrayList<String> tmp = new ArrayList<String>();
tmp.add(tickets[i][1]);
map.put(tickets[i][0], tmp);
}
}
result.add("JFK");
itineraryHelper("JFK", map, result, total, 1);
return result;
}
public boolean itineraryHelper(String current, HashMap<String, ArrayList<String>> map,
ArrayList<String> result, int total, int num){
if(num >= total){
return true;
}
if(!map.containsKey(current) || map.get(current).size() == 0){
return false;
}
ArrayList<String> curList = map.get(current);
int i = 0;
while(i < curList.size()){
String next = curList.remove(i);
result.add(next);
if(itineraryHelper(next, map, result, total, num + 1)){
return true;
}
result.remove(result.size() - 1);
listAdd(next, curList);
i++;
}
return false;
}
public void listAdd(String value, ArrayList<String> list){
if(list.size() == 0){
list.add(value);
return;
} else{
int i = 0;
while(i < list.size()){
if(value.compareTo(list.get(i)) <= 0){
list.add(i, value);
return;
}
i++;
}
list.add(value);
return;
}
}