้็ฎๆฏๆฏ่ผ็นๅฅ็ 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ๆฌก
ๆๆๅทฎๅพๅค