LeetCode #277 Medium

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.
Constraints
  • 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.
two-pointersgraphelimination
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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?

1

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.

2

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.

3

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def findCelebrity(self, n: int) -> int:
▶3 cand = 0
▶4 for i in range(1, n):
▶5 if knows(cand, i):
▶6 cand = i
▶7 for i in range(n):
▶8 if i == cand:
▶9 continue
▶10 if knows(cand, i) or not knows(i, cand):
▶11 return -1
▶12 return cand
pass 1: find a candidate0123candidate0knows calls0at most 9 neededstart with person 0 as the candidate
cand0
calls0
Nobody has been ruled out yet. Person 0 is the first candidate, and each person after them is asked about in turn. Every question rules out exactly one of the two people in it.
pass 1: find a candidate0123candidate0knows calls1at most 9 neededdoes 0 know 1? yes → 0 is out, 1 takes over
i1
knows(0, 1)true
cand1
Person 0 knows person 1. A celebrity knows no one, so 0 is ruled out. Person 1 has not been ruled out, so they become the candidate.
pass 1: find a candidate0123candidate1knows calls2at most 9 neededdoes 1 know 2? yes → 1 is out, 2 takes over
i2
knows(1, 2)true
cand2
Person 1 knows person 2. A celebrity knows no one, so 1 is ruled out. Person 2 has not been ruled out, so they become the candidate.
pass 1: find a candidate0123candidate2knows calls3at most 9 neededdoes 2 know 3? no → 3 is out
i3
knows(2, 3)false
cand2
Person 2 does not know person 3. A celebrity is known by everyone, so 3 is ruled out, and 2 stays the candidate.
pass 2: check the candidate0123candidate2knows calls3at most 9 neededonly 2 is left → now check them
cand2
calls3
Everyone except 2 was ruled out by one question each. That does not prove 2 is a celebrity: nobody has asked whether 2 knows the people before them, or whether everyone knows 2.
pass 2: check the candidate0123candidate2knows calls4at most 9 neededdoes 2 know 0? no → good
i0
knows(2, 0)false
Person 2 does not know person 0, as a celebrity should not. Next: does 0 know 2?
pass 2: check the candidate0123candidate2knows calls5at most 9 neededdoes 0 know 2? yes → 0 passes
i0
knows(0, 2)true
Person 0 knows 2, so this pair is consistent with 2 being the celebrity.
pass 2: check the candidate0123candidate2knows calls6at most 9 neededdoes 2 know 1? no → good
i1
knows(2, 1)false
Person 2 does not know person 1, as a celebrity should not. Next: does 1 know 2?
pass 2: check the candidate0123candidate2knows calls7at most 9 neededdoes 1 know 2? yes → 1 passes
i1
knows(1, 2)true
Person 1 knows 2, so this pair is consistent with 2 being the celebrity.
pass 2: check the candidate0123candidate2knows calls8at most 9 neededdoes 2 know 3? no → good
i3
knows(2, 3)false
Person 2 does not know person 3, as a celebrity should not. Next: does 3 know 2?
pass 2: check the candidate0123candidate2knows calls9at most 9 neededdoes 3 know 2? yes → 3 passes
i3
knows(3, 2)true
Person 3 knows 2, so this pair is consistent with 2 being the celebrity.
pass 2: check the candidate0123candidate2knows calls9at most 9 neededevery check passed → answer is 2
calls9
answer2
Person 2 knows nobody and everyone knows them, so the answer is 2. That took 9 calls, the most this method ever needs for 4 people.
05

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.

▶1class Solution:
▶2 def findCelebrity(self, n: int) -> int:
▶3 lo, hi = 0, n - 1
▶4 while lo < hi:
▶5 if knows(lo, hi):
▶6 lo += 1
▶7 else:
▶8 hi -= 1
▶9 for i in range(n):
▶10 if i != lo and (knows(lo, i) or not knows(i, lo)):
▶11 return -1
▶12 return lo
06

Common pitfalls

Returning the survivor without checking it

✗ Wrong
for i in range(1, n):
    if knows(cand, i):
        cand = i
return cand
✓ Right
for i in range(n):
    if i != cand and (knows(cand, i) or not knows(i, cand)):
        return -1
return cand

The 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

✗ Wrong
for i in range(n):
    if knows(cand, i) or not knows(i, cand):
        return -1
✓ Right
for i in range(n):
    if i == cand:
        continue
    if knows(cand, i) or not knows(i, cand):
        return -1

knows(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

✗ Wrong
if not knows(i, cand):
    return -1
✓ Right
if knows(cand, i) or not knows(i, cand):
    return -1

The 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.

07

Complexity

Time
O(n)
Space
O(1)
n − 1 calls find the candidate and at most 2(n − 1) check it, so at most 3n − 3 calls in all, inside the follow-up's limit of 3n.
08

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.