-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0332.cpp
More file actions
33 lines (28 loc) · 792 Bytes
/
Copy path0332.cpp
File metadata and controls
33 lines (28 loc) · 792 Bytes
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
class Solution {
vector<string> res;
int n;
typedef unordered_map<string, map<string, int>> adj_t;
bool rec(adj_t &adj) {
if (res.size() == n) return true;
const string &name = res.back();
for (auto &next : adj[name]) {
if (!next.second) continue;
res.push_back(next.first);
next.second--;
if (rec(adj)) return true;
next.second++;
res.pop_back();
}
return false;
}
public:
vector<string> findItinerary(const vector<vector<string>> &tickets) {
adj_t adj;
n = tickets.size() + 1;
for (const auto &ticket : tickets)
adj[ticket[0]][ticket[1]]++;
res = {"JFK"};
rec(adj);
return res;
}
};