Leetcode 332: Reconstruct Itinerary

Posted Keep walking

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.

 

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.

 

 1 public class Solution {
 2     public IList<string> FindItinerary(string[,] tickets) {
 3         var dict = new Dictionary<string, List<string>>();
 4         var trips = new Dictionary<Tuple<string, string>, int>();
 5         
 6         for (int i = 0; i < tickets.GetLength(0); i++)
 7         {
 8             if (!dict.ContainsKey(tickets[i, 0]))
 9             {
10                 dict[tickets[i, 0]] = new List<string>();
11             }
12             
13             dict[tickets[i, 0]].Add(tickets[i, 1]);
14             
15             var trip = new Tuple<string, string>(tickets[i, 0], tickets[i, 1]);
16             
17             if (!trips.ContainsKey(trip))
18             {
19                 trips[trip] = 0;
20             }
21             
22             trips[trip]++;
23         }
24         
25         foreach (var item in dict)
26         {
27             if (item.Value != null)
28             {
29                 item.Value.Sort();
30             }
31         }
32         
33         var result = new List<string>(dict.Count);
34         result.Add("JFK");
35         DFS(dict, "JFK", result, trips);
36         
37         return result;
38     }
39     
40     private bool DFS(Dictionary<string, List<string>> dict, string depart, IList<string> result,  Dictionary<Tuple<string, string>, int>  trips)
41     {
42         if (trips.Count == 0) return true;
43         if (!dict.ContainsKey(depart)) return false;
44         
45         foreach (var d in dict[depart])
46         {
47             var trip = new Tuple<string, string>(depart, d);
48             
49             if (trips.ContainsKey(trip) && trips[trip] > 0)
50             {
51                 trips[trip]--;
52                 
53                 if (trips[trip] <= 0) trips.Remove(trip);
54                 
55                 result.Add(d);
56                 if (DFS(dict, d, result, trips))
57                 {
58                     return true;
59                 }
60                 
61                 if (!trips.ContainsKey(trip))
62                 {
63                     trips[trip] = 0;
64                 }
65 
66                 trips[trip]++;
67                 result.RemoveAt(result.Count - 1);
68             }
69         }
70         
71         return false;
72     }
73 }

 






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

[LeetCode] 332. Reconstruct Itinerary

[LeetCode] 332. Reconstruct Itinerary Java

332. Reconstruct Itinerary

Leetcode 332 重新安排行程(欧拉路径)

332. Reconstruct Itinerary

332. Reconstruct Itinerary --- DFS转迭代,用栈