Company: Visa
Difficulty: medium
Minimum Cost to Connect Servers A data centre has n servers numbered from 1 to n . Server i has capacity cap[i] . A link may be built between two different servers in either of the following ways. Direct link. For any two servers u and v , a link between them can be built at a cost of |cap[u] - cap[v]| . 2. Closest-capacity link. For a server u , let d be the smallest value of |cap[u] - cap[w]| over all servers w other than u . For every server v with |cap[u] - cap[v]| = d - that is, for every server whose capacity is closest to the capacity of u - a link between u and v can be built at a cost of 1 . If several servers are equally close, every one of them may be used. Links have no direction: a link between u and v can be traversed either way. Any number of links may be built, and a connection between two servers may pass through as many intermediate servers as you like. The cost of connecting u to v is the total cost of the links used on the route, and the cheapest route is wanted. Yo