Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Traveler {
  4.     public String closest(String[] connections) {
  5.  
  6.         Set<Integer> visited = new HashSet<>();
  7.  
  8.         Map<Integer, List<Integer>> adj = new HashMap<>();
  9.  
  10.         // {"0-1","1-2","2-3","4-5","5-6","3-0"} -> {0: [1, 3], 1: [0, 2], ...}
  11.         for (String c : connections) {
  12.             String[] spl = c.split("-");
  13.  
  14.             int a = Integer.parseInt(spl[0]);
  15.             int b = Integer.parseInt(spl[1]);
  16.  
  17.             List<Integer> aAdj = adj.getOrDefault(a, new ArrayList<>());
  18.             List<Integer> bAdj = adj.getOrDefault(b, new ArrayList<>());
  19.  
  20.             aAdj.add(b);
  21.             bAdj.add(a);
  22.  
  23.             adj.put(a, aAdj);
  24.             adj.put(b, bAdj);
  25.  
  26.         }
  27.  
  28.         LinkedList<Node> q = new LinkedList<>();
  29.         q.add(new Node(0, 0));
  30.         visited.add(0);
  31.  
  32.         // counts how many nodes are on distance key from the 0-th node
  33.         // {0: [1, 2], 1: [0], 2: [3]}
  34.         // {0: 1,   1: 2,   2: 1}
  35.         //  0        1, 2   3 is on distance 2 from 0
  36.         Map<Integer, Integer> nodesCountOnGivenDistance = new TreeMap<>(); // {3: 100, 5: 200}
  37.  
  38.         while (!q.isEmpty()) {
  39.  
  40.             Node current = q.pop();
  41.  
  42.             for (int neighbor : adj.get(current.index)) {
  43.                 if (!visited.contains(neighbor)) {
  44.  
  45.                     Node neighborNode = new Node(neighbor, current.dist + 1);
  46.                     q.add(neighborNode); // (1, 1), (7, 1), (8, 1)  --> poll 7, 1
  47.  
  48.                     int distanceCount = nodesCountOnGivenDistance.getOrDefault(current.dist + 1, 0);
  49.                     nodesCountOnGivenDistance.put(current.dist + 1, distanceCount + 1);
  50.                     visited.add(neighbor);
  51.                 }
  52.             }
  53.         }
  54.  
  55.         StringBuilder sb = new StringBuilder();
  56.  
  57.         for (int count : nodesCountOnGivenDistance.values()) {
  58.             sb.append(String.valueOf(count)).append(",");
  59.         }
  60.  
  61.         return sb.substring(0, sb.length() - 1);
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         System.out.println(new Traveler().closest(new String[]{"0-1", "0-2", "0-3", "0-10", "0-7", "1-8", "1-6", "2-4", "2-5", "2-8", "3-11", "3-10", "4-8", "8-9", "11-12"}));
  66.     }
  67. }
  68.  
  69. class Node {
  70.     int index, dist;
  71.  
  72.     public Node(int index, int dist) {
  73.         this.index = index;
  74.         this.dist = dist;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement