Never use Map if you can use array in Java on LeetCode
Just backtracking solution for Q4, but I use Map<Integer, Integer> visited, then I get a TLE, after the contest, I tried int[] visited, and AC.\n\n\nTLE code\n\
Just backtracking solution for Q4, but I use `Map<Integer, Integer> visited`, then I get a TLE, after the contest, I tried `int[] visited`, and AC.\n\n\nTLE code\n```\nclass Solution {\n    public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {\n        int n = values.length;\n        Map<Integer, Map<Integer, Integer>> edgeMap = new HashMap<>();\n        for(int[] edge: edges){\n            edgeMap.putIfAbsent(edge[0], new HashMap<>());\n            edgeMap.get(edge[0]).put(edge[1], edge[2]);\n            edgeMap.putIfAbsent(edge[1], new HashMap<>());\n            edgeMap.get(edge[1]).put(edge[0], edge[2]);\n        }\n        Map<Integer, Integer> visited = new HashMap<>();\n        visited.put(0, 1);\n        backtrack(values, edgeMap, visited, 0, 0, maxTime, values[0]);\n        return res;\n    }\n    \n    int res = 0;\n    \n    void backtrack(int[] values, Map<Integer, Map<Integer, Integer>> edgeMap, Map<Integer, Integer> visited, int cur, int time, int maxTime, int gain){\n        if(time > maxTime){\n            return;\n        }\n        if(cur == 0){\n            res = Math.max(res, gain);\n        }\n        if(!edgeMap.containsKey(cur)){\n            return;\n        }\n        for(int next: edgeMap.get(cur).keySet()){\n            int cost = edgeMap.get(cur).get(next);\n            int value = visited.containsKey(next) ? 0 : values[next];\n            visited.put(next, visited.getOrDefault(next, 0) + 1);\n            backtrack(values, edgeMap, visited, next, time + cost, maxTime, gain + value);\n            visited.put(next, visited.get(next) - 1);\n            if(visited.get(next) == 0){\n                visited.remove(next);\n            }\n        }\n        return;\n    }\n}\n```
[]