You are given a list of airline tickets
where tickets[i] = [fromi, toi]
represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.
All of the tickets belong to a man who departs from "JFK"
, thus, the itinerary must begin with "JFK"
. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.
- For example, the itinerary
["JFK", "LGA"]
has a smaller lexical order than["JFK", "LGB"]
.
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
Example 1:
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] Output: ["JFK","MUC","LHR","SFO","SJC"]
Example 2:
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] Output: ["JFK","ATL","JFK","SFO","ATL","SFO"] Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] but it is larger in lexical order.
Constraints:
1 <= tickets.length <= 300
tickets[i].length == 2
fromi.length == 3
toi.length == 3
fromi
andtoi
consist of uppercase English letters.fromi != toi
A:
没啥说的, 就是经典的DFS。 比较tricky的地方,就是需要trial,if failed, need reset
class Solution {public:vector<string> findItinerary(vector<vector<string>>& tickets) {unordered_map<string, vector<string>> M;int N = tickets.size();for (auto ticket : tickets) {M[ticket[0]].push_back(ticket[1]);}for (auto& p : M) {sort(p.second.rbegin(), p.second.rend()); // reverse order}vector<string> res{"JFK"};dfs(M, res, N + 1);return res;}private:void dfs(unordered_map<string, vector<string>>& M, vector<string>& res,int N) {string from = res.back();auto& toCities = M[from];for (int i = toCities.size()-1; i>=0 ; i--) {res.push_back(toCities[i]);toCities.erase(toCities.begin() + i);dfs(M, res, N);if (res.size() == N)return;else { // need trial and error)toCities.insert(toCities.begin() + i, res.back());res.pop_back();}}}};
然而, 上面的dfs。LTE 。坏处就是需要删除vector的某个节点
那么,我们不删除, 而只是mark呢?
class Solution {public:vector<string> findItinerary(vector<vector<string>>& tickets) {unordered_map<string, vector<string>> M;int N = tickets.size();for (auto ticket : tickets) {M[ticket[0]].push_back(ticket[1]);}for (auto& p : M) {sort(p.second.rbegin(), p.second.rend()); // reverse order}vector<string> res{"JFK"};dfs(M, res, N + 1);return res;}private:void dfs(unordered_map<string, vector<string>>& M, vector<string>& res,int N) {string from = res.back();auto& toCities = M[from];for (int i = toCities.size() - 1; i >= 0; i--) {if(toCities[i]=="") // instead of erase, we mark itcontinue;res.push_back(toCities[i]);toCities[i] = "";dfs(M, res, N);if (res.size() == N)return;else { // need trial and error)toCities[i] = res.back();res.pop_back();}}}};依然LTE。
看了fail的例子。加了2行
if(i+1 < toCities.size() && toCities[i] == toCities[i+1]) // skil same choicecontinue;最终的pass的代码是:class Solution {public:vector<string> findItinerary(vector<vector<string>>& tickets) {unordered_map<string, vector<string>> M;int N = tickets.size();for (auto ticket : tickets) {M[ticket[0]].push_back(ticket[1]);}for (auto& p : M) {sort(p.second.rbegin(), p.second.rend()); // reverse order}vector<string> res{"JFK"};dfs(M, res, N + 1);return res;}private:void dfs(unordered_map<string, vector<string>>& M, vector<string>& res,int N) {string from = res.back();auto& toCities = M[from];for (int i = toCities.size() - 1; i >= 0; i--) {if(toCities[i]=="") // instead of erase, we mark itcontinue;if(i+1 < toCities.size() && toCities[i] == toCities[i+1]) // same choicecontinue;res.push_back(toCities[i]);toCities[i] = "";dfs(M, res, N);if (res.size() == N)return;else { // need trial and error)toCities[i] = res.back();res.pop_back();}}}};
Mistakes:
Inside your DFS loop, you're doing this:
This creates a copy of the vector, so changes like:
are only applied to the copy, not the actual M[...]
.
As a result:
-
You're never really updating the flight map.
-
Backtracking is not working as intended.
✅ Fix:
You need to access and modify the original vector by reference:
No comments:
Post a Comment