-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstructItinerary.cpp
41 lines (37 loc) · 1.05 KB
/
reconstructItinerary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Source: https://leetcode.com/problems/reconstruct-itinerary/
// Author: Miao Zhang
// Date: 2021-02-03
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
route_.clear();
trips_.clear();
for (const auto& t: tickets) {
trips_[t[0]].push_back(t[1]);
}
for (auto& t: trips_) {
auto& dests = t.second;
std::sort(dests.begin(), dests.end());
}
string kstart = "JFK";
visit(kstart);
return vector<string>(route_.crbegin(), route_.crend());
}
private:
// src -> {dst1, dst2, ..., dstn}
unordered_map<string, deque<string>> trips_;
// res
vector<string> route_;
void visit(string& src) {
auto& dests = trips_[src];
while (!dests.empty()) {
// get the smallest
string dest = dests.front();
// remove
dests.pop_front();
// visit dest
visit(dest);
}
route_.push_back(src);
}
};