Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created May 7, 2018 13:56
Show Gist options
  • Save s4553711/79279478dc30829678989f913c319447 to your computer and use it in GitHub Desktop.
Save s4553711/79279478dc30829678989f913c319447 to your computer and use it in GitHub Desktop.
class Solution {
private:
unordered_map<string, deque<string>> trips_;
vector<string> route_;
public:
vector<string> findItinerary(vector<pair<string, string>> tickets) {
route_.clear();
trips_.clear();
for(const auto& pair: tickets)
trips_[pair.first].push_back(pair.second);
for(auto& pair: trips_) {
auto& dest = pair.second;
std::sort(dest.begin(), dest.end());
}
const string kStart = "JFK";
visit(kStart);
return vector<string>(route_.crbegin(), route_.crend());
}
void visit(const string& src) {
auto& dests = trips_[src];
while (!dests.empty()) {
const string dest = dests.front();
dests.pop_front();
visit(dest);
}
route_.push_back(src);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment