-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path332. Reconstruct Itinerary
48 lines (37 loc) · 1.47 KB
/
332. Reconstruct Itinerary
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
42
43
44
45
46
47
48
class Solution {
private:
// Create an adjacency list to represent the flights
unordered_map<string, vector<string>> flightGraph;
// Store the final itinerary
vector<string> itinerary;
public:
// Depth-First Search to traverse the flight itinerary
void dfs(string airport) {
vector<string> &destinations = flightGraph[airport];
// Visit destinations in lexical order
while (!destinations.empty()) {
string nextDestination = destinations.back();
destinations.pop_back();
dfs(nextDestination);
}
// Add the current airport to the itinerary after visiting all destinations
itinerary.push_back(airport);
}
vector<string> findItinerary(vector<vector<string>>& tickets) {
// Populate the flight graph using ticket information
for (int i = 0; i < tickets.size(); i++) {
string from = tickets[i][0];
string to = tickets[i][1];
flightGraph[from].push_back(to);
}
// Sort destinations in reverse order to visit lexical smaller destinations first
for (auto &entry : flightGraph) {
sort(entry.second.rbegin(), entry.second.rend());
}
// Start the DFS from the JFK airport
dfs("JFK");
// Reverse the itinerary to get the correct order
reverse(itinerary.begin(), itinerary.end());
return itinerary;
}
};