Java DFS with list to sort , Time Complexity(OlevelLoglevel), Space Complexity(On)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    Map<Integer, Long> ans = new HashMap<>();\n    // Map<Integer, TreeSet<Integer>> maxMap = new HashMap<>();\n    // Map<Integer, TreeSet<Integer>> minMap = new HashMap<>();\n    public long[] placedCoins(int[][] edges, int[] cost) {\n        Map<Integer, List<Integer>> map = new HashMap<>();\n        for(int[] edge: edges){\n            List<Integer> list1 = map.getOrDefault(edge[0], new ArrayList<>());\n            list1.add(edge[1]);\n            List<Integer> list2 = map.getOrDefault(edge[1], new ArrayList<>());\n            list2.add(edge[0]);\n            map.put(edge[0], list1);\n            map.put(edge[1], list2);\n        }\n        // for(int j = 0; j < cost.length; j++){\n        //     maxMap.put(j, new TreeSet<Integer>());\n        //     minMap.put(j, new TreeSet<Integer>(Collections.reverseOrder()));\n        // }\n        int[] visit = new int[cost.length];\n        helper(map, 0, cost, visit);\n        long[] res = new long[cost.length];\n        for(int i = 0; i < res.length; i++){\n            res[i] = ans.get(i);\n        }\n        return res; \n    }\n\n    private int[] helper(Map<Integer, List<Integer>> map, int cur, int[] cost, int[] visit){\n        // return int[], 0 is count , 1-2-3 largest positive, 4- 5 smallest negative\n   \n        int[] res = new int[6];\n        res[0] = 1;\n        visit[cur] = 1;\n        List<Integer> listPo = new ArrayList<>();\n        List<Integer> listNe = new ArrayList<>();\n        if(map.containsKey(cur)){\n            for(Integer each: map.get(cur)){\n                if(visit[each] == 0){\n                    int[] next = helper(map, each, cost, visit);\n                    res[0] += next[0];\n                    listPo.add(next[1]);\n                    listPo.add(next[2]);\n                    listPo.add(next[3]);\n                    listNe.add(next[4]);\n                    listNe.add(next[5]);\n                };\n            }\n        }\n        if(cost[cur] > 0){\n            listPo.add(cost[cur]);\n        }else{\n            listNe.add(cost[cur]);\n        }\n        Collections.sort(listPo, (a,b)-> b - a);\n        Collections.sort(listNe, (a,b)-> a - b);\n        \n        if(res[0] < 3){\n            ans.put(cur, 1L);\n        }else{\n            //get max prodcut\n            long maxPru = Long.MIN_VALUE;\n            if(listPo.size() >= 3){\n                long lst = listPo.get(0);\n                long sst = listPo.get(1);\n                long tst = listPo.get(2);\n                maxPru = Long.max(lst * sst * tst, maxPru);\n            }\n            if(listNe.size() >= 2 && listPo.size() >= 1){\n                long sst = listNe.get(0);\n                long ssst = listNe.get(1);\n                long lst = listPo.get(0);\n                maxPru = Long.max(lst * sst * ssst, maxPru);\n            }\n            if(maxPru > 0){\n                ans.put(cur, maxPru);\n            }else{\n                ans.put(cur, 0L);\n            }\n        }\n        for(int m = 0; m < listPo.size(); m++){\n            if(m > 2){\n                break;\n            }\n            res[m + 1] = listPo.get(m);\n        }\n        for(int n = 0; n < listNe.size(); n++){\n            if(n > 1){\n                break;\n            }\n            res[n + 4] = listNe.get(n);\n        }\n        return res;\n    }\n\n\n}\n```
['Java']