Ctrl K

Quietest of the Richer

Medium
There are n people numbered from 0 to n - 1. Everyone has a different amount of money, but the exact sums are unknown: there is only a list of comparisons richer, where the pair [a, b] means that a is richer than b. The comparisons are consistent. Also, volume[i] is how loud person i speaks; all volumes are distinct. For each person x, look at everyone who is *certainly* at least as rich: x themselves and everyone richer than x through a chain of comparisons. Among them, find the quietest one, the one with the smallest volume. Return an array answer of length n where answer[x] is that person's number.
Example 1
richer=[[2, 0], [1, 2], [3, 1]], volume=[1, 2, 3, 0] → [3, 3, 3, 3]

Person 3 is richer than 1, 1 than 2, and 2 than 0, so person 3 is surely at least as rich as each of them. With volume 0 this person is the quietest, so 3 is everyone's answer.

Example 2
richer=[[0, 1], [0, 2]], volume=[2, 0, 1] → [0, 1, 2]

Nobody is known to be richer than person 0, so 0 is their own answer. For 1 the candidates are 0 and 1, and 1 is quieter. For 2 the candidates are 0 and 2, and 2 is quieter.

Example 3
richer=[], volume=[1, 0] → [0, 1]

With no comparisons, everyone is their own answer.

Constraints
  • 1 ≤ n = len(volume) ≤ 10⁴
  • 0 ≤ volume[i] ≤ 10⁴, all values are distinct
  • 0 ≤ len(richer) ≤ n · (n − 1) / 2
  • richer[i] = [a, b], 0 ≤ a, b < n, a ≠ b
  • All pairs in richer are distinct, and the comparisons form no cycle
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsExpand a Template With Variables MediumLetter Order From a Dictionary HardLongest Uphill Walk HardTree Path Without Twin Neighbours HardUphill-Only Walks Hard
loading Python…
Input
richer=[[2, 0], [1, 2], [3, 1]], volume=[1, 2, 3, 0]
Expected
[3, 3, 3, 3]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.