码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 332: Reconstruct Itinerary

时间:2017-12-10 14:25:21      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:value   new   sum   null   port   another   cap   res   pair   

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

标签:value   new   sum   null   port   another   cap   res   pair   

原文地址:http://www.cnblogs.com/liangmou/p/8016609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!