There are n teams numbered from 0 to n - 1 in a tournament; each team is also a node in a DAG.
You are given the integer n and a 0-indexed 2D integer array edges of length m representing the DAG, where edges[i] = [ui, vi] indicates that there is a directed edge from team ui to team vi in the graph.
A directed edge from a to b in the graph means that team a is stronger than team b and team b is weaker than team a.
Team a will be the champion of the tournament if there is no team b that is stronger than team a.
Return the team that will be the champion of the tournament if there is a unique champion, otherwise, return -1.
Notes
a1, a2, ..., an, an+1 such that node a1 is the same node as node an+1, the nodes a1, a2, ..., an are distinct, and there is a directed edge from the node ai to node ai+1 for every i in the range [1, n].Example 1:

Input: n = 3, edges = [[0,1],[1,2]] Output: 0 Explanation: Team 1 is weaker than team 0. Team 2 is weaker than team 1. So the champion is team 0.
Example 2:

Input: n = 4, edges = [[0,2],[1,3],[1,2]] Output: -1 Explanation: Team 2 is weaker than team 0 and team 1. Team 3 is weaker than team 1. But team 1 and team 0 are not weaker than any other teams. So the answer is -1.
Constraints:
1 <= n <= 100m == edges.length0 <= m <= n * (n - 1) / 2edges[i].length == 20 <= edge[i][j] <= n - 1edges[i][0] != edges[i][1]a is stronger than team b, team b is not stronger than team a.a is stronger than team b and team b is stronger than team c, then team a is stronger than team c.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the champion involves checking every possible pairing of teams. We directly simulate each tournament outcome to see which team would win. Essentially, we compare each team against every other team to see if it's a potential champion.
Here's how the algorithm would work step-by-step:
def find_champion_ii_brute_force(number_of_teams, results):
number_of_champions = 0
# Check each team to see if they are a champion
for potential_champion in range(number_of_teams):
is_champion = True
# Iterate through all other teams to verify they lose to the potential champion
for opponent_team in range(number_of_teams):
if potential_champion == opponent_team:
continue
# Check if the opponent team loses to the potential champion according to the results
loses_to_champion = False
for result in results:
if result[0] == potential_champion and result[1] == opponent_team:
loses_to_champion = True
break
# If there's a team that doesn't lose to the potential champion, it's not a real champion
if not loses_to_champion:
is_champion = False
break
# Increment the champion count if the team is a real champion
if is_champion:
number_of_champions += 1
return number_of_championsThe problem identifies a tournament champion. Instead of simulating the entire tournament, we can use the idea that losers can't be champions. The key is to figure out how many players definitely lost at least one match.
Here's how the algorithm would work step-by-step:
def find_champion_ii(number_of_players, matches):
losers = set()
# Identify all players who have lost at least one match.
for match in matches:
loser = match[1]
losers.add(loser)
# Calculate the number of players who haven't lost.
never_lost_count = number_of_players - len(losers)
# If there's exactly one player who hasn't lost,
if never_lost_count == 1:
for player in range(number_of_players):
# Then this is our champion.
if player not in losers:
return player
# Otherwise, return -1 because there is no champion or many.
return -1| Case | How to Handle |
|---|---|
| Null or empty 'edges' list. | Return 0, as no champion can be determined without any edges. |
| An edge that refers to a non-existent node (out of range indices). | Handle this by either ignoring the edge or throwing an exception if the index is out of bounds for 'n'. |
| Self-loop edges (e.g., [0, 0]). | Ignore self-loop edges as a node cannot be defeated by itself. |
| Input 'n' is less than or equal to 0. | Return 0, as no valid champion can exist if there are no nodes. |
| Input 'edges' contains duplicate edges. | The indegree calculation handles this naturally by incrementing indegree multiple times. |
| Cyclic graph where every node defeats another; no single node with in-degree 0 exists. | Return -1 to indicate no champion exists in the cyclic graph. |
| Large input 'n' causing potential memory issues with indegree array. | Ensure the indegree array is initialized with the correct size relative to 'n', avoiding allocation errors. |
| Multiple nodes with an in-degree of zero (more than one potential champion). | Return -1 since there should be exactly one champion as per the problem definition. |