Find Champion II - Problem
Find the Tournament Champion

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 vi

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 -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
๐Ÿ† Tournament Championship Graph01230 beats 11 beats 21 beats 3In-Degree AnalysisTeam 0: 0 defeats โ†’ ๐Ÿ† CHAMPIONTeam 1: 1 defeat (by team 0)Team 2: 1 defeat (by team 1)Team 3: 1 defeat (by team 1)Result: Team 0 is the unique undefeated champion!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Array of size n to store in-degree count for each team

n
2n
โšก 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
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
24.6K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen