-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
38 lines (37 loc) · 1.12 KB
/
solution.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
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
class Solution {
public:
std::vector<std::string> findItinerary(
std::vector<std::vector<std::string>>& tickets) {
std::vector<std::string> ans;
std::unordered_map<
std::string_view,
std::priority_queue<std::string_view,
std::vector<std::string_view>,
std::greater<std::string_view>>>
edges;
for (const auto& ticket : tickets) {
edges[ticket[0]].push(ticket[1]);
}
std::stack<std::string_view> planned;
planned.push("JFK");
while (!planned.empty()) {
std::string_view curr = planned.top();
if (!edges[curr].empty()) {
planned.push(edges[curr].top());
edges[curr].pop();
} else {
ans.push_back(std::string(curr));
planned.pop();
}
}
std::reverse(ans.begin(), ans.end());
return ans;
}
};