[LeetCode] 332. Reconstruct Itinerary

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 332. Reconstruct Itinerary相关的知识,希望对你有一定的参考价值。

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:

  1. 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"].
  2. All airports are represented by three capital letters (IATA code).
  3. You may assume all tickets form at least one valid itinerary.
  4. One must use all the tickets once and only once.

Example 1:

Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]

Example 2:

Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
             But it is larger in lexical order.

重新安排行程。

给定一个机票的字符串二维数组 [from, to],子数组中的两个成员分别表示飞机出发和降落的机场地点,对该行程进行重新规划排序。所有这些机票都属于一个从JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 出发。

说明:

如果存在多种有效的行程,你可以按字符自然排序返回最小的行程组合。例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前
所有的机场都用三个大写字母表示(机场代码)。
假定所有机票至少存在一种合理的行程。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reconstruct-itinerary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是DFS。这是一个图论的问题,首先要做的是把图建起来,会用到hashmap创建邻接表。同时因为题目要求按字符自然排序返回最小的行程组合,所以会需要用到priority queue来帮助排序。建图的时候需要遍历tickets,把出发地from当做key存入hashmap,把所有的目的地to存入pq。接着再用dfs,从始发地JFK去遍历,遍历的时候,先从pq里弹出下一个目的地,然后再递归找目的地的目的地。递归完成之后将地点放入stack。

时间O(nlogn)

空间O(n)

Java实现

 1 class Solution {
 2     public List<String> findItinerary(List<List<String>> tickets) {
 3         List<String> res = new ArrayList<>();
 4         Map<String, PriorityQueue<String>> g = new HashMap<>();
 5         buildGraph(tickets, g);
 6         Deque<String> stack = new ArrayDeque<>();
 7         dfs(g, stack, "JFK");
 8         while (!stack.isEmpty()) {
 9             res.add(stack.pop());
10         }
11         return res;
12     }
13 
14     private void buildGraph(List<List<String>> tickets, Map<String, PriorityQueue<String>> g) {
15         for (List<String> ticket : tickets) {
16             String from = ticket.get(0);
17             String to = ticket.get(1);
18             if (!g.containsKey(from)) {
19                 g.put(from, new PriorityQueue<>());
20             }
21             g.get(from).offer(to);
22         }
23     }
24 
25     private void dfs(Map<String, PriorityQueue<String>> g, Deque<String> stack, String from) {
26         PriorityQueue<String> arrivals = g.get(from);
27         while (arrivals != null && !arrivals.isEmpty()) {
28             String to = arrivals.poll();
29             dfs(g, stack, to);
30         }
31         stack.push(from);
32     }
33 }

 

LeetCode 题目总结

以上是关于[LeetCode] 332. Reconstruct Itinerary的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 332: Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary

leetcode332

[LeetCode] 332. Reconstruct Itinerary Java

LeetCode332. 重新安排行程

LeetCode332. 重新安排行程