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.
Analysis
This is an application of Hierholzer’s algorithm to find a Eulerian path.
PriorityQueue should be used instead of TreeSet, because there are duplicate entries.
Java Solution
public class Solution{ HashMap<String, PriorityQueue<String>> map = new HashMap<String, PriorityQueue<String>>(); LinkedList<String> result = new LinkedList<String>(); public List<String> findItinerary(String[][] tickets) { for (String[] ticket : tickets) { if (!map.containsKey(ticket[0])) { PriorityQueue<String> q = new PriorityQueue<String>(); map.put(ticket[0], q); } map.get(ticket[0]).offer(ticket[1]); } dfs("JFK"); return result; } public void dfs(String s) { PriorityQueue<String> q = map.get(s); while (q != null && !q.isEmpty()) { dfs(q.poll()); } result.addFirst(s); } } |
run this for input
[[“JFK”,”C”],[“C”,”JFK”],[“JFK”,”B”]]
no. This is topographical sort. https://www.programcreek.com/2014/06/leetcode-course-schedule-ii-java/
Isn’t this a way of doing? topological sort.