There are n chess players numbered from 0 to n - 1. You are given the results of some games.
Each result is represented by the string "u,v", meaning player u defeated player v. A smaller numerical rank represents a stronger player, and the stronger player always wins.
Use the known results and their transitive relationships to determine which players have an exact rank.
ChessRankAnalyzer
int countPlayersWithKnownRank(int n, List<String> gameResults)
n: The number of chess players.gameResults: The known game results. Each string has the format "u,v", indicating that player u defeated player v.Return the number of players whose exact rank can be determined.
List<String> getKnownPlayerRanks(int n, List<String> gameResults)
n: The number of chess players.gameResults: The known game results in "winner,loser" format.Return the players whose exact ranks can be determined. Each returned string must have the format "playerId,rank".
Return the strings in ascending order of playerId. Return an empty list if no player's exact rank can be determined.
1 is the strongest rank, and rank n is the weakest rank.u defeated player v, then u has a smaller numerical rank than v.u defeated v and v defeated w, then u ranks above w.1 ≤ n ≤ 1000 ≤ gameResults.size() ≤ n × (n - 1) / 2gameResults has the format "u,v".0 ≤ u < n0 ≤ v < nu != vnull. countPlayersWithKnownRank( n = 5, gameResults = List.of("3,1", "1,4", "4,0", "0,2"))
Output: 5
The results establish the complete order 3, 1, 4, 0, 2, so every player's rank is known.
getKnownPlayerRanks( n = 5, gameResults = List.of("3,1", "1,4", "4,0", "0,2"))
Output: List.of("0,4", "1,2", "2,5", "3,1", "4,3")
countPlayersWithKnownRank( n = 6, gameResults = List.of( "0,2", "1,2", "2,3", "2,4", "3,5", "4,5"))
Output: 2
Players 0 and 1 can appear in either order, as can players 3 and 4. However, player 2 must be third and player 5 must be sixth.
getKnownPlayerRanks( n = 6, gameResults = List.of( "0,2", "1,2", "2,3", "2,4", "3,5", "4,5"))
Output: List.of("2,3", "5,6")
countPlayersWithKnownRank( n = 4, gameResults = List.of("0,2", "1,3"))
Output: 0
The two independent results do not establish any player's position relative to every other player.
getKnownPlayerRanks( n = 4, gameResults = List.of("0,2", "1,3"))
Output: List.of()