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); } }
相关文章推荐
- 动态规划——三角形问题
- 黄聪:HtmlAgilityPack教程案例
- 黄聪:VS2010每次F5启动都重新编译但是没办法进入断点
- dp 多重背包
- 机器学习系列03——K近邻算法(KNN)
- 来到c语言世界
- PHP 获取指定日期的星期方法如下
- dp 完全背包
- jdbc
- 内存管理--拷贝
- tomcat 服务开机自启动
- 自定义控件之滑动开关
- 派(Pie,NWERC 2006,LA 3635)
- 备忘录模式——编程中的后悔药
- spring ioc 容器概念
- Java中的设计模式
- 2.4决策树之决策树实例
- Python成长之路第二篇(1)_数据类型内置函数用法
- Codeforces Round #342 (Div. 2) C
- 造轮子:SQL拼接方法