Skip to content

Commit 121b458

Browse files
committed
New Problem Solution "Reconstruct Itinerary"
1 parent 86f9497 commit 121b458

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ LeetCode
5454
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/) | [C++](./algorithms/cpp/countingBits/CountingBits.cpp)|Medium|
5555
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
5656
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
57+
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | [C++](./algorithms/cpp/reconstructItinerary/ReconstructItinerary.cpp)|Medium|
5758
|331|[Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [C++](./algorithms/cpp/verifyPreorderSerializationOfABinaryTree/VerifyPreorderSerializationOfABinaryTree.cpp)|Medium|
5859
|330|[Patching Array](https://leetcode.com/problems/patching-array/) | [C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium|
5960
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/) | [C++](./algorithms/cpp/longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrix.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Source : https://leetcode.com/problems/reconstruct-itinerary/
2+
// Author : Hao Chen
3+
// Date : 2017-01-06
4+
5+
/***************************************************************************************
6+
*
7+
* Given a list of airline tickets represented by pairs of departure and arrival
8+
* airports [from, to], reconstruct the itinerary in order. All of the tickets belong
9+
* to a man who departs from JFK. Thus, the itinerary must begin with JFK.
10+
*
11+
* Note:
12+
*
13+
* If there are multiple valid itineraries, you should return the itinerary that has
14+
* the smallest lexical order when read as a single string. For example, the itinerary
15+
* ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
16+
* All airports are represented by three capital letters (IATA code).
17+
* You may assume all tickets form at least one valid itinerary.
18+
*
19+
* Example 1:
20+
* tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
21+
* Return ["JFK", "MUC", "LHR", "SFO", "SJC"].
22+
*
23+
* Example 2:
24+
* tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
25+
* Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
26+
* Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it
27+
* is larger in lexical order.
28+
*
29+
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
30+
* cases.
31+
***************************************************************************************/
32+
33+
/*
34+
This problem's description really confuse me.
35+
36+
for examples:
37+
1) [["JFK", "PEK"], ["JFK", "SHA"], ["SHA", "JFK"]], which has two itineraries:
38+
a) JFK -> PEK,
39+
b) JFK -> SHA -> JFK -> PEK
40+
The a) is smaller than b), because PEK < SHA, however the b) is correct answer.
41+
So, it means we need use all of tickets.
42+
43+
2) [["JFK", "PEK"], ["JFK", "SHA"]], which also has two itineraries:
44+
a) JFK -> PEK
45+
b) JFK -> SHA
46+
for my understanding, the JFK -> SHA is the correct one,
47+
however, the correct answer is JFK -> SHA -> PEK.
48+
I don't understand, why the correct answer is not JFK -> PEK -> SHA
49+
That really does not make sense to me.
50+
51+
All right, we need assume all of the tickets can be connected in one itinerary.
52+
Then, it's easy to have a DFS algorithm.
53+
*/
54+
55+
56+
class Solution {
57+
public:
58+
//DFS
59+
void travel(string& start, unordered_map<string, multiset<string>>& map, vector<string>& result) {
60+
while (map[start].size() > 0 ) {
61+
string next = *(map[start].begin());
62+
map[start].erase(map[start].begin());
63+
travel(next, map, result);
64+
}
65+
result.insert(result.begin(), start);
66+
}
67+
68+
vector<string> findItinerary(vector<pair<string, string>> tickets) {
69+
unordered_map<string, multiset<string>> map;
70+
for(auto t : tickets) {
71+
map[t.first].insert(t.second);
72+
}
73+
vector<string> result;
74+
string start = "JFK";
75+
travel(start, map, result);
76+
return result;
77+
}
78+
};

0 commit comments

Comments
 (0)