> ## 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 Celebrity Python Solution with Tests

> Tested Python solution for LeetCode 277 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 277, [Medium](/catalog/medium). Topics: [Graph](/catalog/topics/graph), [Two Pointers](/catalog/topics/two-pointers), [Interactive](/catalog/topics/interactive). [View on LeetCode](https://leetcode.com/problems/find-the-celebrity/description/).

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

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

## Problem

Suppose you are at a party with `n` people labeled from `0` to `n - 1` and among them, there may exist one celebrity. The definition of a celebrity is that all the other `n - 1` people know the celebrity, but the celebrity does not know any of them.

Now you want to find out who the celebrity is or verify that there is not one. You are only allowed to ask questions like: "Hi, A. Do you know B?" to get information about whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).

You are given an integer `n` and a helper function `bool knows(a, b)` that tells you whether `a` knows `b`. Implement a function `int findCelebrity(n)`. There will be exactly one celebrity if they are at the party.

Return *the celebrity's label if there is a celebrity at the party*. If there is no celebrity, return `-1`.

**Note** that the `n x n` 2D array `graph` given as input is **not** directly available to you, and instead **only** accessible through the helper function `knows`. `graph[i][j] == 1` represents person `i` knows person `j`, whereas `graph[i][j] == 0` represents person `i` does not know person `j`.

**Follow up:** If the maximum number of allowed calls to the API `knows` is `3 * n`, could you find a solution without exceeding the maximum number of calls?

### Examples

![Example 1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0277.Find%20the%20Celebrity/images/g1.jpg)

```
Input: graph = [[1,1,0],[0,1,0],[1,1,1]]
Output: 1
Explanation: There are three persons labeled with 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is the person labeled as 1 because both 0 and 2 know him but 1 does not know anybody.
```

![Example 2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0277.Find%20the%20Celebrity/images/g2.jpg)

```
Input: graph = [[1,0,1],[1,1,0],[0,1,1]]
Output: -1
Explanation: There is no celebrity.
```

### Constraints

* `n == graph.length == graph[i].length`
* `2 <= n <= 100`
* `graph[i][j]` is `0` or `1`.
* `graph[i][i] == 1`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from typing import ClassVar


class Solution:
    # Backing store for the knows API, injected by the test helper
    graph: ClassVar[list[list[int]]] = []

    # Time: O(n) knows calls
    # Space: O(1)
    def knows(self, a: int, b: int) -> bool:
        return bool(self.graph[a][b])

    # Time: O(n) knows calls
    # Space: O(1)
    def find_celebrity(self, n: int) -> int:
        candidate = 0
        for other in range(1, n):
            if self.knows(candidate, other):
                candidate = other
        for other in range(n):
            if other == candidate:
                continue
            if self.knows(candidate, other) or not self.knows(other, candidate):
                return -1
        return candidate
```

## Complexity

| Time | Space |
| - | - |
| O(n) knows calls | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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