> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Find Champion II Python Solution with Tests

> Tested Python solution for LeetCode 2924 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2924, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Graph Theory](/catalog/topics/graph-theory). [View on LeetCode](https://leetcode.com/problems/find-champion-ii/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2924   # by problem number
lcpy gen -s find_champion_ii   # by problem name
```

## Problem

There are `n` teams numbered from `0` to `n - 1` in a tournament; each team is also a node in a \<strong>DAG\</strong>.

You are given the integer `n` and a \<strong>0-indexed\</strong> 2D integer array `edges` of length `m` representing the \<strong>DAG\</strong>, where `edges[i] = [u_i, v_i]` indicates that there is a directed edge from team `u_i` to team `v_i` in the graph.

A directed edge from `a` to `b` in the graph means that team `a` is \<strong>stronger\</strong> than team `b` and team `b` is \<strong>weaker\</strong> than team `a`.

Team `a` will be the \<strong>champion\</strong> of the tournament if there is no team `b` that is \<strong>stronger\</strong> than team `a`.

Return \<em>the team that will be the \<strong>champion\</strong> of the tournament if there is a \<strong>unique\</strong> champion, otherwise, return \</em>\<code>-1\</code>\<em>.\</em>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2023/10/19/graph-3.png)

```
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](https://assets.leetcode.com/uploads/2023/10/19/graph-4.png)

```
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 \<= 100
* m == edges.length
* 0 \<= m \<= n \* (n - 1) / 2
* `edges[i].length == 2`
* 0 \<= edges\[i]\[j] \<= n - 1
* `edges[i][0] != edges[i][1]`
* The input is generated such that if team `a` is stronger than team `b`, team `b` is not stronger than team `a`.
* The input is generated such that if team `a` is stronger than team `b` and team `b` is stronger than team `c`, then team `a` is stronger than team `c`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_champion_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/find_champion_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m)
    # Space: O(n)
    def find_champion(self, n: int, edges: list[list[int]]) -> int:
        weaker_count = [0] * n
        for _stronger, weaker in edges:
            weaker_count[weaker] += 1

        champions = [team for team in range(n) if weaker_count[team] == 0]
        return champions[0] if len(champions) == 1 else -1
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.