Find the Celebrity Problem
Description
LeetCode Problem 277.
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, wherease graph[i][j] == 0 represents person j does not know person i.
Example 1:

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:

Input: graph = [[1,0,1],[1,1,0],[0,1,1]]
Output: -1
Explanation: There is no celebrity.
Constraints:
n == graph.length == graph[i].length2 <= n <= 100graph[i][j]is0or1.graph[i][i] == 1
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?
Solution
Approach: Eliminate Candidates, Then Verify
Asking about every pair takes n2 calls. One question can rule out a person much faster: for any two people a and b, knows(a, b) rules out one of them.
- If
aknowsb, thenais not the celebrity (the celebrity knows nobody). - If
adoes not knowb, thenbis not the celebrity (everybody knows the celebrity).
Pass 1: start with candidate = 0 and compare it with everybody else in turn. If knows(candidate, i), i becomes the new candidate; otherwise i is ruled out. After n - 1 calls, everybody except candidate has been ruled out, so if a celebrity exists, it is candidate.
Pass 2: the candidate is only someone nobody has ruled out, not a sure celebrity. Check it against everyone i: the candidate must not know i, and i must know the candidate. If any check fails, there is no celebrity and we return -1.
This second pass is easy to forget: pass 1 always ends with some candidate. In Example 2 it ends with person 2, but 2 knows 1, so there is no celebrity.
Complexity: O(n) time, with at most 3(n - 1) calls to knows (n - 1 in pass 1 and at most 2 per person in pass 2). O(1) extra space.
class Solution:
def findCelebrity(self, n: int) -> int:
# Pass 1: if the candidate knows i, the candidate is not the celebrity, but i might be.
# If not, i is not the celebrity (someone does not know them). One candidate survives.
candidate = 0
for i in range(1, n):
if knows(candidate, i):
candidate = i
# Pass 2: check that the candidate knows nobody and everybody knows the candidate.
for i in range(n):
if i != candidate and (knows(candidate, i) or not knows(i, candidate)):
return -1
return candidate