> ## 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 the Town Judge Python Solution with Tests

> Tested Python solution for LeetCode 997 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 997, Easy. Topics: Array, Hash Table, Graph Theory. [View on LeetCode](https://leetcode.com/problems/find-the-town-judge/description/).

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

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

## Problem

In a town, there are `n` people labeled from `1` to `n`. There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.

You are given an array `trust` where `trust[i] = [ai, bi]` representing that the person labeled `ai` trusts the person labeled `bi`. If a trust relationship does not exist in `trust` array, then such a trust relationship does not exist.

Return *the label of the town judge if the town judge exists and can be identified, or return* `-1` *otherwise*.

### Examples

```
Input: n = 2, trust = [[1,2]]
Output: 2
```

```
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
```

```
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
```

### Constraints

* 1 \<= n \<= 1000
* 0 \<= trust.length \<= 10^4
* `trust[i].length == 2`
* All the pairs of `trust` are **unique**.
* `ai != bi`
* 1 \<= ai, bi \<= n

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + t) where t = len(trust)
    # Space: O(n)
    def find_judge(self, n: int, trust: list[list[int]]) -> int:
        # Net trust score: indegree - outdegree. Judge must reach n - 1.
        trust_score = [0] * (n + 1)
        for truster, trusted in trust:
            trust_score[truster] -= 1
            trust_score[trusted] += 1

        for person in range(1, n + 1):
            if trust_score[person] == n - 1:
                return person
        return -1
```

## Complexity

| Time                          | Space |
| ----------------------------- | ----- |
| O(n + t) where t = len(trust) | O(n)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
