Find Champion II - Problem
Find the Tournament Champion
Imagine a tournament with
You're given:
โข An integer
โข A 2D array
Goal: Find the tournament champion - the team that no other team has defeated. If there's exactly one such team, return its number. If there are multiple undefeated teams or no clear champion, return
The champion is essentially the team with no incoming edges in the graph!
Imagine a tournament with
n teams numbered from 0 to n-1. The tournament results form a Directed Acyclic Graph (DAG) where each directed edge represents one team defeating another.You're given:
โข An integer
n - the number of teamsโข A 2D array
edges where edges[i] = [ui, vi] means team ui defeated team viGoal: Find the tournament champion - the team that no other team has defeated. If there's exactly one such team, return its number. If there are multiple undefeated teams or no clear champion, return
-1.The champion is essentially the team with no incoming edges in the graph!
Input & Output
example_1.py โ Basic Tournament
$
Input:
n = 3, edges = [[0,1],[1,2]]
โบ
Output:
0
๐ก Note:
Team 0 defeated team 1, and team 1 defeated team 2. Team 0 has never been defeated by anyone, making it the unique champion.
example_2.py โ No Clear Champion
$
Input:
n = 4, edges = [[0,2],[1,3],[1,2]]
โบ
Output:
-1
๐ก Note:
Teams 0 and 1 both have never been defeated. Since there are multiple undefeated teams, there's no unique champion.
example_3.py โ Single Team
$
Input:
n = 1, edges = []
โบ
Output:
0
๐ก Note:
With only one team and no matches, team 0 is automatically the champion.
Visualization
Tap to expand
Understanding the Visualization
1
Build the Tournament Graph
Each directed edge shows which team defeated which other team
2
Count Defeats for Each Team
Track how many incoming edges (losses) each team has
3
Find the Champion
The champion has exactly zero losses (in-degree = 0)
Key Takeaway
๐ฏ Key Insight: The tournament champion is simply the team with in-degree 0 in the directed graph - no other team has defeated them!
Time & Space Complexity
Time Complexity
O(m + n)
O(m) to process all edges + O(n) to find champion among all teams
โ Linear Growth
Space Complexity
O(n)
Array of size n to store in-degree count for each team
โก Linearithmic Space
Constraints
- 1 โค n โค 100
- 0 โค edges.length โค n * (n - 1) / 2
- edges[i].length == 2
- 0 โค edge[i][j] โค n - 1
- edge[i][0] โ edge[i][1]
- The input is generated such that edges represent a valid DAG
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code