Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- *
- [
- [3, 0],
- [0, 4],
- [5, 0],
- [2, 1],
- [1, 4],
- [2, 3],
- [5, 2]
- ]
- *
- *
- */
- int [] getChildren(int s, int[][] roads, int p){
- List<Integer> children = new ArrayList<>();
- for(int i = 0; i < roads.length; i++){
- if(roads[i][0] == s && roads[i][1] != p){
- children.add(roads[i][1]);
- }
- if(roads[i][1] == s && roads[i][0] != p){
- children.add(roads[i][0]);
- }
- }
- return children
- .stream()
- .mapToInt(Integer::intValue)
- .toArray();
- }
- boolean efficientRoadNetwork(int n, int[][] roads) {
- // 0 to n
- for(int i = 0; i < n; i++){
- int total = 0;
- int[] children = getChildren(i, roads, -1);
- System.out.println("children");
- // this is a one level tree
- if(children.length + 1 == n){
- return true;
- }
- total += children.length;
- Arrays.stream(children).forEach(System.out::println);
- // check second level only
- for(int j = 0; j < children.length; j++){
- int[] innerChildren = getChildren(children[j],roads,i);
- System.out.println("inner children");
- total += innerChildren.length;
- }
- if(total < n - 1){
- return false;
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment