You are viewing a single comment's thread. Return to all comments →
Java solution
public static List<Integer> componentsInGraph(List<List<Integer>> gb) { Map<Integer, Set<Integer>> map = new HashMap<>(); for(List<Integer> line : gb) { int p1 = line.get(0); int p2 = line.get(1); Set<Integer> s1 = map.get(p1); Set<Integer> s2 = map.get(p2); if(s1 == null || s2 == null) { Set<Integer> s = (s1 == null) ? s2 : s1; if(s == null) { s = new HashSet<>(); } s.add(p1); s.add(p2); map.put(p1, s); map.put(p2, s); } else { if(s1 != s2) { s1.addAll(s2); for(int i: s2) { map.put(i, s1); } map.put(p2, s1); } } } int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(Set<Integer> s: map.values()) { min = Math.min(min, s.size()); max = Math.max(max, s.size()); } return Arrays.asList(min, max); }
Seems like cookies are disabled on this browser, please enable them to open this website
Components in a graph
You are viewing a single comment's thread. Return to all comments →
Java solution