Hierarchy
You are given a 0-indexed integer array qualifications representing the qualification level of each employee in a company. You are also given a 2D integer array applications where applications[i] = [u, v, cost] indicates that employee u is willing to supervise employee v for the given cost.
A valid company hierarchy is a directed tree where exactly one employee (the root) has no supervisor, and every other employee has exactly one supervisor. Furthermore, an employee can only supervise someone if their qualification is strictly greater than the subordinate's (qualifications[u] > qualifications[v]).
Return the minimum total cost to build a valid hierarchy. If it is impossible to build one, return -1.
Example 1:
Input: qualifications = [7, 5, 3], applications = [[0, 1, 10], [0, 2, 100], [1, 2, 5]]
Output: 15
Explanation:
Employee 0 (qual = 7) supervises Employee 1 (qual = 5) for cost 10.
Employee 1 (qual = 5) supervises Employee 2 (qual = 3) for cost 5.
Total cost = 10 + 5 = 15. Exactly one person (0) has no supervisor.
Example 2:
Input: qualifications = [10, 10, 5], applications = [[0, 2, 5], [1, 2, 5]]
Output: -1
Explanation:
Employees 0 and 1 both have the maximum qualification of 10. Neither can have a supervisor. Since a valid hierarchy allows only one person without a supervisor, it is impossible.
Now Your Turn!
Pick the correct output for the given inputInput: qualifications = [10, 8, 6, 4], applications = [[0, 1, 5], [1, 2, 5], [2, 3, 5], [0, 3, 20]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= qualifications.length <= 1000
- 0 <= qualifications[i] <= 106
- 0 <= applications.length <= 10000
- applications[i].length == 3
- 0 <= applications[i][0], applications[i][1] < qualifications.length
- 0 <= applications[i][2] <= 106
- qualifications[applications[i][0]] > qualifications[applications[i][1]]