codekofi
← All problems

Problem 47

Hard

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1void walk(map<string, multiset<string>>& out, const string& at,
2 vector<string>& route) {
3
4 auto& ahead = out[at];
5
6 while (!ahead.empty()) {
7
8 string other = *ahead.begin();
9 ahead.erase(ahead.begin());
10
11 walk(out, other, route);
12 }
13
14 route.push_back(at);
15}
16
17vector<string> solution(const vector<vector<string>>& tickets) {
18
19 map<string, multiset<string>> out;
20
21 for (const auto& t : tickets) {
22 out[t[0]].insert(t[1]);
23 }
24
25 vector<string> route;
26 walk(out, "JFK", route);
27
28 reverse(route.begin(), route.end());
29
30 return route;
31}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.