Find the Celebrity
Find the Celebrity is LeetCode 277 (Medium). At a party of n people labelled 0 to n − 1, a celebrity is someone who is known by everyone else and knows no one else. The only way to learn anything is the helper knows(a, b), which says whether person a knows person b. Return the celebrity's label, or −1 if there is none.
- There can be at most one celebrity: two celebrities would each have to know the other, and a celebrity knows no one.
- Asking about every pair takes n(n − 1) calls. The follow-up asks for at most 3n.
- Everyone counts as knowing themselves:
knows(i, i)is true.
- n == graph.length == graph[i].length
- 2 <= n <= 100
- graph[i][j] is 0 or 1.
- graph[i][i] == 1
- Follow-up: use at most 3 * n calls to knows.
Intuition
Every call to knows(a, b) rules one person out, whatever the answer. If a knows b, then a knows someone, so a is not the celebrity. If a does not know b, then not everyone knows b, so b is not the celebrity.
So n − 1 well-chosen calls leave a single person who has not been ruled out. That survivor is only a candidate: the calls showed that nobody else can be the celebrity, not that the survivor is one. A second pass asks the candidate's questions directly and answers −1 if any of them fails.
Find the one item that relates to all others in a fixed way, using a yes/no query on pairs: look for a query that eliminates one item whichever way it answers, then verify the survivor.
Approach
Before reading on, take one call knows(a, b) and work out who is ruled out when it says yes, and who when it says no. How many calls does it take to leave one person? What do you still have to check about that person?
Two ways to find the candidate
Ask knows(cand, i) for each next person and hand the candidate role to i on a yes.
- One loop: a single index to track.
- Same calls: n − 1 to find the candidate.
Ask whether the left person knows the right one; a yes rules out the left, a no rules out the right. Stop when they meet.
- Same idea: one elimination per call.
- Familiar: common when the input is a full matrix.
Both rule out one person per call and then run the same check, in O(n) time and O(1) space. The steps, code and live demo use the single candidate; the two-pointer code follows the demo.
Pass a candidate along the line
Start with cand = 0. For each i from 1 to n − 1, ask knows(cand, i). If yes, the candidate knows someone and is out, so i becomes the candidate. If no, i is unknown to the candidate and is out, so the candidate stays.
Why the survivor is the only possibility
Each of the n − 1 calls ruled out one person, and nobody is ruled out twice, so everyone except the final cand has been ruled out. If a celebrity exists, it is cand.
Check the candidate against everyone
For every other person i, the candidate must not know i, and i must know the candidate. If either test fails, return −1; if all pass, return cand. Skip i == cand, because knows(cand, cand) is true and would reject a real celebrity.
Code in Python, C++ and Java
Two pointers from both ends
lo starts at the first person and hi at the last. If lo knows hi, lo cannot be the celebrity, so lo moves right; otherwise hi is not known by everyone, so hi moves left. When they meet, that person is the only candidate, and the same check as before decides the answer.
Common pitfalls
Returning the survivor without checking it
for i in range(1, n):
if knows(cand, i):
cand = i
return candfor i in range(n):
if i != cand and (knows(cand, i) or not knows(i, cand)):
return -1
return candThe first pass always leaves someone, even at a party with no celebrity. On [[1,0,1],[1,1,0],[0,1,1]] it leaves person 2, who knows person 1, so the answer should be −1.
Asking whether the candidate knows themselves
for i in range(n):
if knows(cand, i) or not knows(i, cand):
return -1for i in range(n):
if i == cand:
continue
if knows(cand, i) or not knows(i, cand):
return -1knows(i, i) is true for everyone, so the check fails on the candidate itself and the code returns −1 every time. On [[1,1,0],[0,1,0],[1,1,1]] person 1 is the celebrity, but this returns −1.
Checking only that everyone knows the candidate
if not knows(i, cand):
return -1if knows(cand, i) or not knows(i, cand):
return -1The first pass only tested the candidate against people after it. When everyone knows everyone, as in [[1,1],[1,1]], person 1 survives and everyone knows them, but they know person 0, so there is no celebrity.
Complexity
Find the Celebrity FAQ
Why not test each person in turn?
Checking one person takes 2(n − 1) calls, and there are n people, so that is about 2n² calls, roughly 20,000 for 100 people instead of at most 297. It is correct, but it misses the 3n follow-up.
Can the check use fewer calls?
Yes. Once the final candidate took over, the first pass asked knows(cand, i) for every later i and got no each time. So the check only needs knows(cand, i) for people before the candidate, and knows(i, cand) for everyone.
What if I am given the whole matrix instead of a knows function?
Read M[a][b] wherever the code calls knows(a, b). The steps do not change, and the answer still reads only O(n) of the n² entries.