[JAVA] Detailed Solution with Handwritten Explanation
Runtime - 11ms - Faster than 69.49%\n\nclass Solution {\n    int[][] par;\n    \n    public int minMalwareSpread(int[][] graph, int[] initial) {\n    \tint n = 
Runtime - 11ms - Faster than 69.49%\n```\nclass Solution {\n    int[][] par;\n    \n    public int minMalwareSpread(int[][] graph, int[] initial) {\n    \tint n = graph.length;\n        par = new int[n][2];\n        for(int i = 0; i < par.length; i++){\n            par[i][0] = i;\n            par[i][1] = 1;\n        }\n        \n        HashSet<Integer> set = new HashSet<>();\n        for(int p : initial)\n        \tset.add(p);\n        \n        for(int i = 0; i < n; i++) {\n        \tfor(int j = i + 1; j < n; j++) {\n        \t\tif(graph[i][j] == 1 && !set.contains(i) && !set.contains(j)) {\n        \t\t\tint li = find(i);\n        \t\t\tint lj = find(j);\n        \t\t\tif(li != lj){\n                        par[li][0] = lj;\n                        par[lj][1] += par[li][1];\n                    }\n        \t\t}\n        \t}\n        }\n        \n        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();\n        HashMap<Integer, HashSet<Integer>> conntoinf = new HashMap<>();\n        \n        for(int i : initial) {\n        \tmap.put(i, new HashSet<>());\n        \tfor(int j = 0; j < n; j++) {\n        \t\tif(graph[i][j] == 1 && i != j && !set.contains(j)) {\n        \t\t\tint lj = find(j);\n        \t\t\tmap.get(i).add(lj);\n                    if(conntoinf.containsKey(lj) == false)\n                        conntoinf.put(lj, new HashSet<>());\n        \t\t\tconntoinf.get(lj).add(i);\n        \t\t}\n        \t}\n        }\n        int ans = initial[0];\n        int max = -1;\n        \n        for(int p : initial) {\n        \tint val = 0;\n        \tfor(int l : map.get(p)) {\n        \t\tif(conntoinf.get(l).size() == 1)\n        \t\t\tval += par[l][1];\n        \t}\n            if(val >= max) {\n                if(val == max)\n                    ans = Math.min(p, ans);\n                else\n                    ans = p;\n                max = val;\n            }\n        }\n        \n        return ans;\n    }\n    \n    public int find(int x){\n        if(par[x][0] == x)\n            return x;\n        int temp = find(par[x][0]);\n        return par[x][0] = temp;\n    }\n}\n```\n\n**Handwritten Explanation**\n\nPlease first solve https://leetcode.com/problems/minimize-malware-spread/, to understand this solution properly.\nSee https://leetcode.com/problems/minimize-malware-spread/discuss/1704842/JAVA-Detailed-Solution-with-Handwritten-Explanation, for the Solution of above problem.\n\n![image](https://assets.leetcode.com/users/images/7654c13c-014d-4b53-94da-a5546ca62880_1642741151.0972898.jpeg)\n
['Java']