Fixed Ranks
GoogleOnsiteMedium
There are n players numbered 0 to n - 1. Each game result is a [winner, loser] pair meaning the winner ranks strictly better than the loser, with rank 0 the best. Better-than carries over: if a beat b and b beat c, then a ranks better than c even if they never played.
Part 1: Determined ranks
You are given n players numbered 0 to n - 1 and a list game_results of [winner, loser] pairs. Each pair says the winner ranks strictly better than the loser, and this carries over transitively: if a beat b and b beat c, then a ranks better than c even if they never played.
A player's rank is the number of players that rank strictly better than them. Return a [player, rank] pair for every player whose rank is the same in every complete ordering of the n players that agrees with all results, sorted by player. Leave out players whose rank can vary between orderings.
- Example 1
- n
5- game_results
[[0,1],[1,2],[2,3],[2,4],[3,4]]- Output
[[0,0],[1,1],[2,2],[3,3],[4,4]]
Why: The results form a chain 0, 1, 2, 3, 4 (2 beats 4 is implied by 2 beats 3 beats 4), so every player is comparable with every other and each player's rank equals the number of players above them in the chain.
Constraints
1 <= n <= 5000 <= len(game_results) <= n * (n - 1) / 20 <= winner, loser < nandwinner != loser- No two results in
game_resultscontradict each other - The same
[winner, loser]pair may appear more than once ingame_results