332. Reconstruct Itinerary

้€™็ฎ—ๆ˜ฏๆฏ”่ผƒ็‰นๅˆฅ็š„ backtracking

่ณ‡ๆ–™็ตๆง‹็พๅœจ่ฆๆ”นๆˆ็”จ Map<String, Map<String, Integer>> graph

backtracking -> remove node, add node

ๆ‰ๆœƒ้Ž, ไธ็„ถๆœ€ๅพŒไธ€ๅ€‹ test case ้ƒฝๆœƒ TLE (้กๅค–็”จไธ€ๅ€‹ used map boolean array ็š„่ฉฑ๏ผ‰

T: O(E^d) , E number of filghts (edges), d is the branch numbers of each backtracking

S: O(E), E number of filghts (edges), map needs from -> to

```java
class Solution {
    public List<String> findItinerary(List<List<String>> tickets) {
        Map<String, Map<String, Integer>> graph = buildGraph(tickets);
        return dfs("JFK", graph); // start point is "JFK"
    }
    private List<String> dfs(String start, Map<String, Map<String, Integer>> graph) {
        if (graph.isEmpty()) { // when no nodes on graph, just return with start
            return new ArrayList<>(Arrays.asList(start));
        }
        List<String> next = new ArrayList<>(graph.getOrDefault(start, new HashMap<>()).keySet());
        Collections.sort(next); // sort (for smallest lexical order)

        for (String nxt : next) {
            removeNode(start, nxt, graph); // backtracking
            List<String> result = dfs(nxt, graph);
            addNode(start, nxt, graph); // backtracking

            if (result != null) { // always add start in the beginning
                result.add(0, start);
                return result;
            }
        }
        return null;
    }

    private Map<String, Map<String, Integer>> buildGraph(List<List<String>> tickets) {
        Map<String, Map<String, Integer>> graph = new HashMap<>();
        for (List<String> t : tickets) {
            String from = t.get(0);
            String to = t.get(1);
            addNode(from, to, graph);
        }
        return graph;
    }
    private void addNode(String from, String to, Map<String, Map<String, Integer>> graph) {
        graph.putIfAbsent(from, new HashMap<>());
        Map<String, Integer> map = graph.get(from);
        map.put(to, map.getOrDefault(to, 0) + 1);
    }
    private void removeNode(String from, String to, Map<String, Map<String, Integer>> graph) {
        Map<String, Integer> map = graph.get(from);
        map.put(to, map.getOrDefault(to, 0) - 1);
        if (map.get(to) == 0) {
            map.remove(to);
        }
        if (graph.get(from).isEmpty()) {
            graph.remove(from);
        }
    }
}
```

why ้€™ๅ€‹่งฃๆณ•ๅฏไปฅ้Žๅ‘ข๏ผŸ

ๅ› ็‚บๆœ€ๅพŒไธ€ๅ€‹ tes case, ๅ…ถๅฏฆๅ……ๆปฟไบ†โ€œ้‡่ค‡็š„ flights (from -> to)โ€

ๆ‰€ไปฅๅ…ถๅฏฆ็”จไบ†ไธ€ๅ€‹ ๏ผญap<from, Map<to, count>>

ๅฏไปฅๅคงๅน…ๆธ›ๅฐ‘้‡่ค‡ case ๅŽป่จˆ็ฎ—, ไปฅ "ATL", "AAA" ้‡่ค‡ไบ†้€™้บผๅคšๆฌก, ๆˆ‘ๅ€‘ๅช้œ€่ฆ add, remove backtracking ไธ€ๆฌกๅฐฑๅฏไปฅ้”ๅˆฐๆ•ˆๆžœ

ๅฆ‚ๆžœๆ˜ฏ็”จ Map<key, Boolean[]> ->ๅฆ‚ๆžœ "ATL", "AAA" ้‡่ค‡ไบ†100ๆฌก, ้€™ๆจฃ่ฆ backtracking 100ๆฌก

ๆ•ˆๆžœๅทฎๅพˆๅคš

Last updated