332. Reconstruct itinerary

Source: Internet
Author: User

ref:https://leetcode.com/problems/reconstruct-itinerary/

Given A list of airline tickets represented by pairs of departure and arrival airports [from, to] , reconstruct the itinerary in Order. All of the tickets belong to a man departs from JFK . Thus, the itinerary must begin with JFK .

Note:

    1. If There is multiple valid itineraries, you should return the itinerary that have the smallest lexical order when read as A single string. For example, the itinerary have ["JFK", "LGA"] a smaller lexical order than ["JFK", "LGB"] .
    2. All airports is represented by three capital letters (IATA code).
    3. Assume all tickets form at least one valid itinerary.

Example 1:
tickets=[["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"] .

Example 2:
tickets=[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"] .
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"] . But it's larger in lexical order.

First feeling is normal backtracking (DFS), but when writing crazy in corner case error

The problem is,

1. Such as JFK->SFO, can also have Jfk->atl, can also have SFO->JFK, that is, can have the same journey several times Daoteng, but if only with ordinary map, it is difficult to detect duplicate tickets.

2. If you use the index of tickets[][] inside, every time to tickets to look over to the beginning too slow, will tle

So one answer is well written: https://discuss.leetcode.com/topic/36721/ Short-c-dfs-iterative-44ms-solution-with-explanation-no-recursive-calls-no-backtracking/3

This is the point inside:

1. I think this problem is special, I know this will not have a dead end, that is, these tickets are definitely right, as long as along the <from, to> walk, it must be right, but it may not be lexical order on the optimal, so I think there is no wrong Path to Fallback

2. Use map<string, priorityqueue<stiring>> to achieve the same station greedy the way, as long as the head, is right

Also a bit of their own, is to see a message that stack this has been officially labeled Java Deprecated Class, the official now for queue and stack proposed uniform use of deque.

Push () = Offerfirst () in the stack. Pop () = Pollfirst (), peek () = Peekfirst ()

Add () in queue () = Offerlast (), remove () = Pollfirst (), peek () = Peekfirst ()

I omitted add/remove a group, directly with Offer/poll, the first group also exists, here does not write

1      PublicList<string>finditinerary (string[][] tickets) {2list<string> res =NewArraylist<string>();3         if(Tickets.length = = 0 | | tickets[0].length = = 0) {4             returnRes;5         }6map<string, priorityqueue<string>> map =NewHashmap<>();7          for(inti = 0; i < tickets.length; i++) {8             if(Map.containskey (tickets[i][0])) {9Map.get (Tickets[i][0]). Add (tickets[i][1]);Ten}Else { OnePriorityqueue<string> PQ =NewPriorityqueue<>(); APq.offer (tickets[i][1]); -Map.put (tickets[i][0], PQ); -             } the         } -deque<string> stack =NewArraydeque<>(); -Stack.offerfirst ("JFK"); -          while(!Stack.isempty ()) { +String next =Stack.peekfirst (); -             if(Map.containskey (next) &&!Map.get (Next). IsEmpty ()) { + Stack.offerfirst (Map.get (Next) poll ()); A}Else { at Res.add (Stack.pollfirst ()); -             } -         } - Collections.reverse (res); -         returnRes; -}

332. Reconstruct itinerary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.