[LeetCode] 332. Reconstruct Itinerary

You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.

All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". 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"].

You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.

Example 1:

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

Example 2:

Input: tickets = [["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.

Constraints:

  • 1 <= tickets.length <= 300
  • tickets[i].length == 2
  • fromi.length == 3
  • toi.length == 3
  • fromi and toi consist of uppercase English letters.
  • fromi != toi

重新安排行程。

给你一份航线列表 tickets ,其中 tickets[i] = [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。

所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。

例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前。
假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。

来源:力扣(LeetCode)
链接:https://leetcode.cn/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 题目总结

posted @ 2020-06-30 01:50  CNoodle  阅读(172)  评论(0编辑  收藏  举报