Middle-题目101:332. Reconstruct Itinerary

原创 2016年05月31日 19:54:10

题目原文:
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.
题目大意:
给出一系列机票的列表,用一个二元组[from,to]表示。重新构造出一个从”JFK”出发的旅行线路。如果有多个解,求字典序最小的。
题目分析:
先将机票列表构造成邻接表,其中邻接列表部分使用优先队列维护字典序,然后从”JFK”点开始搜索,每次取出优先队列的队首节点(字典序最小)并再次搜索,直到某个节点的优先队列为空时将当前节点加入线路列表的头部(因为最后一个节点也是最后一个旅行到的节点,应是在表尾的)
源码:(language:java)

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

    public List<String> findItinerary(String[][] tickets) {
        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);
    }
}

成绩:
12ms,beats 81.78%,众数14ms,12.37%
Cmershen的碎碎念:
一开始使用的朴素的dfs解法,每次搜到解则比较字典序,并更新列表,但会超时。而这个算法的本质是贪心法,因为每次拿出字典序最小的点时,如果有解则一定是最优解。(假设邻接表中某个节点对应优先队列是(…p1,…p2…),p1

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode 332. Reconstruct Itinerary

题目【难度:Medium】:Given a list of airline tickets represented by pairs of departure and arrival airports...

[leetcode] 332. Reconstruct Itinerary 解题报告

题目链接:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...

LeetCode 332. Reconstruct Itinerary【medium】

原题: Given a list of airline tickets represented by pairs of departure and arrival airports [fro...
  • o555ddd
  • o555ddd
  • 2016年10月02日 16:42
  • 113

欧拉通路332. Reconstruct Itinerary

解: 欧拉通路:遍历所有的边的一条路径 存在的充要条件: 2个点一个点出度大于入度(起始点),一个点入度大于出度(终止点) 或 所有点出度=入度(欧拉回路) 欧拉回路:遍历所有的边并回到初始...

Leetcode 332. Reconstruct Itinerary

332. Reconstruct Itinerary Total Accepted: 19256 Total Submissions: 71899 Difficulty: Medium Giv...

Leetcode——310 Minimum Height Trees && 332 Reconstruct Itinerary

本周记录两道图论的题目,第一道是关于BFS的题目,310, Minimum Height Trees ;第二道是关于DFS的题目,332,Reconstruct Itinerary。 一、Minim...

332. Reconstruct Itinerary 这道我还真没怎么掌握

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to],...

Leetcode: 332.Reconstruct Itinerary

Leetcode: 332.Reconstruct Itinerary

LeetCode 332 : Reconstruct Itinerary

Reconstruct Itinerary该题是常规的图搜索相关的题目,使用dfs便可以解决,可惜自己还是折腾了好久。 先贴上我原先的代码:#include #include #include ...

LeetCode 332. Reconstruct Itinerary(重构行程)

原题网址:https://leetcode.com/problems/reconstruct-itinerary/ Given a list of airline tickets represen...
  • jmspan
  • jmspan
  • 2016年04月29日 08:06
  • 615
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目101:332. Reconstruct Itinerary
举报原因:
原因补充:

(最多只允许输入30个字)