Find Champion II

Medium Topics Company Tags

There are n teams labeled 0 through n - 1. The directed edge [u, v] means that team u is stronger than team v. The given directed graph has no cycles. Strength is transitive: if one team is stronger than a second, which is stronger than a third, then the first is also stronger than the third.

A champion is a team with no stronger team. Return its label if exactly one champion exists; otherwise return -1.

Edges describe strength relationships, and a sequence of edges also establishes which teams are stronger. Isolated teams are allowed.

Example 1:

Find Champion II, example 1: illustration of the example

Input: n = 4
edges = [[2,0],[0,1],[2,3]]

Output: 2

Explanation: Team 2 is stronger than teams 0 and 3 directly, and stronger than team 1 through team 0. No team is stronger than team 2, making it the unique champion.

Example 2:

Find Champion II, example 2: illustration of the example

Input: n = 3
edges = [[0,2],[1,2]]

Output: -1

Explanation: Teams 0 and 1 are both stronger than team 2, but neither has a stronger team. There are two candidates, so no unique champion exists.

Example 3:

Find Champion II, example 3: illustration of the example

Input: n = 1
edges = []

Output: 0

Explanation: With just one team and no edges, team 0 has no stronger opponent and is the unique champion.

Constraints:

  • 1 <= n <= 100
  • 0 <= edges.length <= n * (n - 1) / 2
  • edges[i].length == 2
  • Edge endpoints are distinct team labels between 0 and n - 1.
  • Edges are distinct, and the directed graph has no cycles.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.