FastPrepCount Determinable Player Rankings

Count Determinable Player Rankings

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

There are n players labeled from 0 through n - 1. Each row wins[i] = [winner, loser] records that winner defeated loser.

Results are transitive: if player A defeated player B, and player B defeated player C, then A ranks above C.

A player's strict rank is determinable when, for every other player, exactly one of these facts is inferable:

  • The player ranks above the other player.
  • The other player ranks above the player.

If both directions are reachable because of a contradictory cycle, that pair does not establish a strict ordering. Return the number of players whose strict rank is determinable.

Function

countDeterminablePlayers(n: int, wins: int[][]) → int

Examples

Example 1

n = 3wins = [[0,1],[1,2]]return = 3

Transitivity establishes 0 > 1 > 2, so every player's relation to both others is known in exactly one direction.

Example 2

n = 4wins = [[0,1],[0,2],[1,3],[2,3]]return = 2

Player 0 is above everyone and player 3 is below everyone. Players 1 and 2 are incomparable.

Example 3

n = 3wins = [[0,1]]return = 0

Every player has at least one unknown relation involving player 2.

Example 4

n = 3wins = [[0,1],[1,0],[1,2]]return = 1

Players 0 and 1 form a contradictory cycle, so neither has a strict rank. Player 2 is below both and is determinable.

Constraints

  • 1 <= n <= 300.
  • 0 <= wins.length <= n * (n - 1).
  • Every row in wins contains two valid, distinct player indices.
  • The same directed result may appear more than once and has the same effect as one occurrence.
  • Contradictory cycles may occur.

More Google problems

See Google hiring insights
public int countDeterminablePlayers(int n, int[][] wins) {
    // Return how many players have a strict relation to every other player.
}
n3
wins[[0,1],[1,2]]
expected3
Checking account…