Ad Network Connectivity Check
Inmobi manages a vast ad delivery network, comprising numerous ad servers interconnected globally. To ensure efficient campaign deployment, rapid incident response, and optimal resource allocation, it's crucial to quickly assess the connectivity between different ad servers. This problem simulates checking the minimum number of direct connections (hops) required to send data from a source server to a target server within this network.
Implement a function that calculates the minimum number of hops (direct connections) needed to travel from a given startServer to a targetServer within the ad network. If the targetServer is unreachable from the startServer, return -1.
n: An integer, representing the total number of ad servers, labeled from 0 to n-1.connections: An array of arrays, where each inner array [u, v] indicates a direct, bidirectional connection between server u and server v.startServer: An integer, the index of the starting server.targetServer: An integer, the index of the target server.u is connected to v, v is also connected to u).startServer equals targetServer.Examples
Example 1
Input:
n = 5, connections = [[0,1],[0,2],[1,3],[2,4]], startServer = 0, targetServer = 3
Output:
2
Note:
The shortest path from server 0 to server 3 is 0 -> 1 -> 3, which takes 2 hops.
Example 2
Input:
n = 4, connections = [[0,1],[2,3]], startServer = 0, targetServer = 3
Output:
-1
Note:
Server 3 is not reachable from server 0.
Example 3
Input:
n = 3, connections = [[0,1],[1,2]], startServer = 1, targetServer = 1
Output:
0
Note:
The start server is the same as the target server, so 0 hops are needed.
Constraints
1 <= n <= 1000 <= connections.length <= n * (n - 1) / 20 <= u, v < n, u != v0 <= startServer, targetServer < nLoading editor...