-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconstruct-itinerary.cpp
42 lines (37 loc) · 1.32 KB
/
reconstruct-itinerary.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
42
class Solution {
public:
// ??????????
// ?????????https://leetcode.com/problems/reconstruct-itinerary/discuss/78768/Short-Ruby-Python-Java-C++/83576
// https://leetcode-cn.com/problems/reconstruct-itinerary/solution/javadfsjie-fa-by-pwrliang/
// ?????????????????????????????
vector<string> res;
unordered_map<string,vector<string>> um;
vector<string> findItinerary(vector<vector<string>>& tickets) {
for(const auto &t:tickets)
um[t[0]].push_back(t[1]);
auto cmp=[](string &a, string &b){
return a>b;
};
for(auto &n:um)
sort(n.second.begin(),n.second.end(),cmp);
string cur="JFK";
res.push_back(cur);
dfs(tickets.size()+1,cur);
return res;
}
bool dfs(int target, string cur){
// cout<<cur<<" "<<res.size()<<" "<<target<<endl;
if(res.size()==target) return true;
if(um[cur].empty()) return false;
auto tmp=um[cur];
for(int i=um[cur].size()-1;i>=0;i--){
string next=um[cur][i];
um[cur].erase(um[cur].begin()+i);
res.push_back(next);
if(dfs(target,next)) return true;
res.pop_back();
um[cur].insert(um[cur].begin()+i,next);
}
return false;
}
};