Skip to content

Commit 1012fa0

Browse files
authored
Create Reconstruct Itinerary
1 parent fda5ff7 commit 1012fa0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Reconstruct Itinerary

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> map;
3+
4+
vector<string> ans;
5+
void dfs(string from)
6+
{
7+
auto &x=map[from];
8+
9+
while(!x.empty())
10+
{
11+
string to=x.top();
12+
x.pop();
13+
dfs(to);
14+
}
15+
ans.push_back(from);
16+
}
17+
18+
public:
19+
20+
vector<string> findItinerary(vector<vector<string>>& tickets)
21+
{
22+
23+
for(auto &x:tickets)
24+
map[x[0]].push(x[1]);
25+
26+
dfs("JFK");
27+
reverse(ans.begin(), ans.end());
28+
return ans;
29+
30+
}
31+
};

0 commit comments

Comments
 (0)