Service Dependency Load Factors
Learn this problemProblem statement
A collection of services forms a directed acyclic dependency graph. The string names[i] identifies service i, and each pair [u, v] in dependencies means that service u calls service v once for every unit of load received by u.
Exactly one unit of external load enters service entry. Every service passes each unit it receives to each of its dependencies. Contributions arriving along different paths add together.
Return one string of the form name load for each service reachable from entry, including entry itself. Use one space between the service name and its decimal load, and sort results lexicographically by service name. Omit unreachable services entirely.
The entry service has load 1. A service called by two different loaded services can have load greater than 1, even though it appears only once in the output. Dependencies belonging to unreachable services contribute no load.
Function
serviceLoads(names: String[], dependencies: int[][], entry: int) → String[]Examples
Example 1
names = ["api","billing","search","db","unused"]dependencies = [[0,1],[0,2],[1,3],[2,3]]entry = 0return = ["api 1","billing 1","db 2","search 1"]The entry api gives one unit to both billing and search. Each sends one unit to db, so its load is 2. The service unused is omitted.
Example 2
names = ["target","root","unused"]dependencies = [[1,0],[2,0]]entry = 1return = ["root 1","target 1"]Only root receives external load. It sends one unit to target; unused contributes zero even though it also has an edge to target.
Constraints
1 <= names.length <= 60.- Service names are distinct and contain
1through20lowercase English letters. 0 <= dependencies.length <= 500.- Every edge contains two distinct valid service indices. No directed edge is repeated, and the entire graph is acyclic.
0 <= entry < names.length.- All load counts fit in a signed
64-bit integer.