332. 重新安排行程

给你一份航线列表 tickets ,其中 tickets[i] = [fromi, toi] 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。

所有这些机票都属于一个从 JFK(肯尼迪国际机场)出发的先生,所以该行程必须从 JFK 开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。

例如,行程 ["JFK", "LGA"] 与 ["JFK", "LGB"] 相比就更小,排序更靠前。
假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reconstruct-itinerary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

回溯

import java.util.*;

class Solution {

    private int total;

    private Map<String, Map<String, Integer>> timesMap;

    private Map<String, TreeSet<String>> graphMap;

    private List<String> ans = new ArrayList<>();

    private LinkedList<String> path = new LinkedList<>();

    private boolean solve(String from) {
        if (path.size() == total) {
            ans.addAll(path);
            return true;
        }

        TreeSet<String> toMap = graphMap.getOrDefault(from, new TreeSet<>());
        Map<String, Integer> toTimeMap = timesMap.getOrDefault(from, new HashMap<>());

        for (String to : toMap) {
            int times = toTimeMap.getOrDefault(to, 0);
            if (times > 0) {
                if (times == 1) {
                    toTimeMap.remove(to);
                } else {
                    toTimeMap.put(to, times - 1);
                }
                path.offerLast(to);
                if (solve(to)) {
                    return true;
                }
                path.pollLast();
                toTimeMap.put(to, times);
            }
        }
        return false;
    }

    public List<String> findItinerary(List<List<String>> tickets) {
        if (tickets == null || tickets.size() == 0) {
            return Collections.emptyList();
        }

        Map<String, TreeSet<String>> graphMap = new HashMap<>();
        Map<String, Map<String, Integer>> timeMap = new HashMap<>();

        for (List<String> ticket : tickets) {
            graphMap.computeIfAbsent(ticket.get(0), k -> new TreeSet<>()).add(ticket.get(1));
            Map<String, Integer> toMap = timeMap.computeIfAbsent(ticket.get(0), k -> new HashMap<>());
            toMap.put(ticket.get(1), toMap.getOrDefault(ticket.get(1), 0) + 1);
        }
        this.total = tickets.size() + 1;
        this.graphMap = graphMap;
        this.timesMap = timeMap;
        path.offerLast("JFK");
        solve("JFK");
        return ans;
    }
}

Hierholzer 算法

import java.util.*;

class Solution {
    Map<String, PriorityQueue<String>> map = new HashMap<String, PriorityQueue<String>>();
    List<String> itinerary = new LinkedList<String>();

    public List<String> findItinerary(List<List<String>> tickets) {
        for (List<String> ticket : tickets) {
            String src = ticket.get(0), dst = ticket.get(1);
            if (!map.containsKey(src)) {
                map.put(src, new PriorityQueue<String>());
            }
            map.get(src).offer(dst);
        }
        dfs("JFK");
        Collections.reverse(itinerary);
        return itinerary;
    }

    public void dfs(String curr) {
        while (map.containsKey(curr) && map.get(curr).size() > 0) {
            String tmp = map.get(curr).poll();
            dfs(tmp);
        }
        itinerary.add(curr);
    }
}
posted @ 2022-01-10 23:42  Tianyiya  阅读(32)  评论(0)    收藏  举报