leetcode332 (re-arranged itinerary: Euler access)

Given a two-dimensional string array [from, to] of a ticket, the two members in the sub-array respectively represent the airport locations of the plane's departure and landing, and the itinerary is re-planned and sorted. All these tickets belong to a gentleman departing from JFK (JFK International Airport), so the journey must start from JFK.

Note: If there are multiple valid itineraries, you can return the smallest itinerary combination in natural order by characters. For example, the itinerary ["JFK", "LGA"] is smaller than ["JFK", "LGB"] and ranks higher.
All airports are represented by three capital letters (airport code).
It is assumed that all tickets have at least one reasonable itinerary.

Example 1:
Input: [[“MUC”, “LHR”], [“JFK”, “MUC”], [“SFO”, “SJC”], [“LHR”, “SFO”]]
Output: [“ JFK", "MUC", "LHR", "SFO", "SJC"]


Solution: Convert the itinerary of the airplane into a directed graph, and then find the Euler path with the smallest natural order value of the characters in the directed graph. In order to minimize the natural ordering value of the characters in the Euler path, we need to use a priority queue (heap) to store other nodes connected to the nodes in the graph, so that the character dictionary will be selected every time the next node is selected At the same time, we have to use the hierholzer algorithm to find an Euler path.

Hierholzer algorithm:
1. Starting from a certain point, perform a depth-first search while maintaining a stack.

2. Every time you move from a vertex to another vertex along an edge, you need to delete this edge (it is better to use a queue to store the node to which the edge of the node is connected for easy deletion).

3. If there is no movable path, add the node where it is to the stack.

4. Pop the elements in the stack into the array to get the result

class Solution {
    
    
    //list数组用于存储结果,map用于存储图信息
    List<String>list=new ArrayList<>();
    HashMap<String,PriorityQueue<String>>map=new HashMap<>();
    public List<String> findItinerary(List<List<String>> tickets) {
    
    
             //填充图信息于map中
             for(List<String> x:tickets) {
    
    
                 String Pos = x.get(0);
                 String next = x.get(1);
                 if (!map.containsKey(Pos))
                     map.put(Pos, new PriorityQueue<>(new Comparator<String>() {
    
    
                         @Override
                         public int compare(String o1, String o2) {
    
    
                             return o1.compareTo(o2);
                         }
                     }));
                 map.get(Pos).offer(next);
             }
             DFS("JFK");
             Collections.reverse(list);
             return list;
    }
    private void DFS(String curr){
    
    
        //直到到达的结点没有出边,再将该结点存储结果数组
        while(map.containsKey(curr)&&map.get(curr).size()>0){
    
    
            String temp=map.get(curr).poll();
            DFS(temp);
        }
       list.add(curr);
    }
}

Guess you like

Origin blog.csdn.net/CY2333333/article/details/108267377