Skip to content

Commit c231707

Browse files
committed
feat: solve No.332
1 parent 05b2420 commit c231707

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

301-400/332. Reconstruct Itinerary.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 332. Reconstruct Itinerary
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Depth-First Search, Graph, Eulerian Circuit.
5+
- Similar Questions: Longest Common Subpath, Valid Arrangement of Pairs.
6+
7+
## Problem
8+
9+
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.
10+
11+
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.
12+
13+
14+
15+
- For example, the itinerary `["JFK", "LGA"]` has a smaller lexical order than `["JFK", "LGB"]`.
16+
17+
18+
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
19+
20+
 
21+
Example 1:
22+
23+
![](https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg)
24+
25+
```
26+
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
27+
Output: ["JFK","MUC","LHR","SFO","SJC"]
28+
```
29+
30+
Example 2:
31+
32+
![](https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg)
33+
34+
```
35+
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
36+
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
37+
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
38+
```
39+
40+
 
41+
**Constraints:**
42+
43+
44+
45+
- `1 <= tickets.length <= 300`
46+
47+
- `tickets[i].length == 2`
48+
49+
- `fromi.length == 3`
50+
51+
- `toi.length == 3`
52+
53+
- `fromi` and `toi` consist of uppercase English letters.
54+
55+
- `fromi != toi`
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {string[][]} tickets
64+
* @return {string[]}
65+
*/
66+
var findItinerary = function(tickets) {
67+
tickets.sort((a, b) => (a[1] === b[1] ? 0 : (a[1] < b[1] ? -1 : 1)));
68+
69+
var map = {};
70+
for (var i = 0; i < tickets.length; i++) {
71+
if (!map[tickets[i][0]]) map[tickets[i][0]] = [];
72+
map[tickets[i][0]].push(tickets[i][1]);
73+
}
74+
75+
console.log(map, tickets);
76+
77+
var itinerary = [];
78+
dfs('JFK', map, itinerary);
79+
return itinerary.reverse();
80+
};
81+
82+
var dfs = function(airport, map, itinerary) {
83+
while (map[airport]?.length) {
84+
dfs(map[airport].shift(), map, itinerary);
85+
}
86+
itinerary.push(airport);
87+
};
88+
```
89+
90+
**Explain:**
91+
92+
nope.
93+
94+
**Complexity:**
95+
96+
* Time complexity : O(n * log(n)).
97+
* Space complexity : O(n).

0 commit comments

Comments
 (0)