685. Quietest Richer Person
A startup has n employees numbered 0 to n - 1. Each pair [a, b] in richer states that employee a earns strictly more than employee b. The statements never contradict each other (there is no cycle), may repeat, and do not cover every pair of people. The array quiet holds distinct values: quiet[i] is how quiet employee i is, and a smaller value means a quieter person.
For every employee x, find the quietest employee among everyone known to earn at least as much as x (reachable through chains of statements), including x. Return an array answer where answer[x] is that employee's number.
Aim for O(n + m) time.
Example 1
- Input:
- richer = [[3,1],[1,0],[2,0],[4,2]], quiet = [6,3,5,1,4]
- Output:
- [3,3,4,3,4]
- Explanation:
Everyone richer than or equal to person 0 includes persons 1, 2, 3 and 4, and person 3 is the quietest of them; person 2 is helped only by person 4.
Example 2
- Input:
- richer = [], quiet = [2,0,1]
- Output:
- [0,1,2]
- Explanation:
No statements, so everyone is the quietest among just themselves.
Constraints
1 ≤ n ≤ 100000
0 ≤ richer.length ≤ 200000
richer[i] = [a, b] with 0 ≤ a, b < n and a ≠ b; the statements contain no cycle
quiet.length = n; the values in quiet are distinct integers in the range [0, 109]
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n + m)
- Space
- O(n + m)