-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path332.cpp
More file actions
executable file
·56 lines (47 loc) · 1.78 KB
/
332.cpp
File metadata and controls
executable file
·56 lines (47 loc) · 1.78 KB
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
49
50
51
52
53
54
55
56
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
map<string,list<string>> linkedlist;
map<string,int> inorder;
int number_of_city = 1;
for (int i = 0; i < tickets.size(); i++){
if (inorder.count(tickets[i][1]))
inorder[tickets[i][1]]++;
else
inorder[tickets[i][1]] = 1;
linkedlist[tickets[i][0]].push_back(tickets[i][1]);
if (tickets[i][1] == 'JFK')
number_of_city = 0;
//when JFK can be the target, which means inorder JFK has a number we shall let number of city be zero
}
number_of_city += inorder.size();
//find all cities
vector<string> res;
res.push_back("JFK");
vector<string> last_round;
unordered_set<string> choosen;
choosen.insert("JFK");
list<int>:: it;
for (it = linkedlist["JFK"].begin(); it != linkedlist["JFK"].end(); it++){
inorder[*it] --;
last_round.push_back(*it);
}
while (res.size() < number_of_city){
int mini = INT_MAX;
string city_choosen = 'unknown';
for (auto s: last_round)
if (inorder[s] < mini && !choosen.count(s)){
mini = inorder[s];
city_choosen = s;
}
res.push_back(city_choosen);
choosen.insert(city_choosen);
last_round.clear();
for (it = linkedlist[city_choosen].begin(); it != linkedlist[city_choosen].end(); it++){
inorder[*it] --;
last_round.push_back(*it);
}
}
return res;
}
};