Order Employees by a Reports-To Hierarchy
Problem statement
names[i] has title titles[i]. Each reportsTo row is [subordinateTitle, managerTitle]. Define a title's depth as the number of reporting links to a top title.
Return employee names sorted by descending title depth. Preserve original order among employees with equal depth.
Function
orderEmployees(names: String[], titles: String[], reportsTo: String[][]) → String[]Examples
Example 1
names = ["John","Sally","Sam","Drax","Bob","Daniel"]titles = ["Manager","CTO","CEO","Engineer","CFO","Engineer"]reportsTo = [["CTO","CEO"],["Manager","CTO"],["Engineer","Manager"],["CFO","CEO"]]return = ["Drax","Daniel","John","Sally","Bob","Sam"]Engineer is deepest, then Manager, then CTO/CFO peers, then CEO.
Constraints
- The title mapping is acyclic and each subordinate title has at most one manager.
- At most
10^5employees.