您的位置:首页 > 其它

Leetcode: Reconstruct Itinerary

2016-02-08 12:29 507 查看
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 may 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.


refer to Recursion https://leetcode.com/discuss/84702/share-my-solution

and Iteration https://leetcode.com/discuss/84706/share-solution-java-greedy-stack-15ms-with-explanation

Explanation https://leetcode.com/discuss/84659/short-ruby-python-java-c



First keep going forward until you get stuck. Put the stuck element always at the front of the result list. Try if it is possible to travel to other places from the airport on the way.

From JFK we first visit JFK -> A -> C -> D -> A. There we're stuck, so we write down A as the end of the route and retreat back to D. There we see the unused ticket to B and follow it: D -> B -> C -> JFK -> D. Then we're stuck again, retreat and write down the airports while doing so: Write down D before A, then JFK before D, the c before JFK, etc. When we're back from our cycle at D, the written route is D -> B -> C -> JFK -> D -> A. Then we retreat further along the original path, prepending C, A and finally JFK to the route, ending up with the route JFK -> A -> C -> D -> B -> C -> JFK -> D -> A.

Since the problem asks for lexical order smallest solution, we can put the neighbors in a min-heap. In this way, we always visit the smallest possible neighbor first in our trip.

Use LinkedList as the result type because we always add at the front of the list

public class Solution {
LinkedList<String> res;
Map<String, PriorityQueue<String>> mp;

public List<String> findItinerary(String[][] tickets) {
if (tickets==null || tickets.length==0) return new LinkedList<String>();
res = new LinkedList<String>();
mp = new HashMap<String, PriorityQueue<String>>();
for (String[] ticket : tickets) {
if (!mp.containsKey(ticket[0])) {
mp.put(ticket[0], new PriorityQueue<String>());
}
mp.get(ticket[0]).offer(ticket[1]);
}
dfs("JFK");
return res;
}

public void dfs(String cur) {
while (mp.containsKey(cur) && !mp.get(cur).isEmpty()) {
dfs(mp.get(cur).poll());
}
res.addFirst(cur);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: