Readable and Maintainable code | Java | Easy to understand | Beginner friendly
Intuition\n\n//initial intuition\nBased on the questino, It\'s just how many nodes can we reach from a given node. So, a simple BFS would give us the result of 
# Intuition\n\n//initial intuition\nBased on the questino, It\'s just how many nodes can we reach from a given node. So, a simple BFS would give us the result of how many nodes we can travel in an undirected graph given a node "i".\n\n//greedy\nSince we can connect a node "i" from graph1 to any node in grahp2, its is always best to connect to the best possible node in graph2 right ? So, yes, greedily we can just take the best possible node in graph2. Because once we know this, we can always connect any node in graph1 to always the best possible node in graph2.\n\n# Approach\nBFS + greedy + graph creation\n\n# Complexity\nIf N is number of nodes in graph1 and M is number of nodes in graph2, then \n- Time complexity:\nn^2 + m^2 + m + n\n\n- Space complexity:\nFor graph -> n + m (since undirected graph, we need to store u,v and v,u)\nFor BFS Queue -> h (height of graph)\ntotally -> n + m + h\n\n# Code\n```java []\nclass Solution {\n    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {\n        Map<Integer,List<Integer>> graph1 = new HashMap<>();\n        Map<Integer,List<Integer>> graph2 = new HashMap<>();\n\n        createGraph(graph1, edges1);\n        createGraph(graph2, edges2);\n\n        //check best node in graph2\n        int maxInGraph2 = getMaxTargetFromGraph2(graph2, k-1);\n\n        int n = graph1.size();\n        int[] result = new int[n];\n        for(int i=0;i<n;i++){\n            int nodesVisited = bfs(i, graph1, k) + maxInGraph2;\n            result[i]=nodesVisited;\n        }\n        \n        return result;\n\n    }\n\n    private int getMaxTargetFromGraph2(Map<Integer,List<Integer>> graph, int k){\n        if(k==-1){\n            return 0;\n        }\n        int n = graph.size();\n        int max = 1;\n        for(int i=0;i<n;i++){\n            int nodes = bfs(i, graph, k);\n            max = Math.max(max, nodes);\n        }\n        return max;\n    }\n\n    private int bfs(int node, Map<Integer,List<Integer>> graph, int k){\n        int nodesVisited = 1;\n        Queue<int[]> q = new LinkedList<>();\n        q.add(new int[]{node,-1});\n        while(!q.isEmpty() && k>0){\n            int n = q.size();\n            k--;\n            for(int i=0;i<n;i++){\n                int[] curr = q.poll();\n                for(int neighbor : graph.get(curr[0])){\n                    if(curr[1]==neighbor) continue;\n                    q.add(new int[]{neighbor,curr[0]});\n                    nodesVisited++;\n                }\n            }\n        }\n        return nodesVisited;\n    }\n\n    private void createGraph(Map<Integer,List<Integer>> graph, int[][] edges){\n        for(int[] edge: edges){\n            graph.putIfAbsent(edge[0], new ArrayList<>());\n            graph.putIfAbsent(edge[1], new ArrayList<>());\n            graph.get(edge[0]).add(edge[1]);\n            graph.get(edge[1]).add(edge[0]);\n        }\n    }\n}\n```
['Greedy', 'Breadth-First Search', 'Graph', 'Java']