Find the minimum cost to connect fromServer[i] to toServer[i] for each query.
There are n servers with distinct capacities in the array capacity. The distance between servers i and j is |capacity[i] - capacity[j]|; each server's closest server is unique. Operations on server x: connect to any server y at cost |capacity[x] - capacity[y]|, or connect to the closest server of x for a fixed cost of 1 unit. Connections can be direct or routed through other servers. Complete the function getMinCost(int capacity[n], int fromServer[m], int toServer[m]) returning int[m]: the minimum cost per query. Constraints: 1 <= n, m <= 2*10^5; 1 <= capacity[i] <= 10^9. Example: capacity = [2, 7, 10], queries 0->2, 1->2, 2->1 -> [2, 1, 1]. Sample: capacity = [2, 3, 5, 6], fromServer = [0, 2, 0], toServer = [3, 0, 1] -> [4, 3, 1].
Format · Microsoft OA.


