Find the Celebrity

Medium

Topics
Two PointersGraphInteractive

Among n people at a party, there may be one celebrity: somebody who everybody else knows, and who knows nobody else.

The acquaintance relation is given as an n x n matrix grid, where grid[a][b] == 1 means person a knows person b. Everybody knows themselves, so grid[i][i] == 1 always, and the diagonal carries no information.

Return the celebrity’s label, or -1 if there is no celebrity. At most one can exist.

On LeetCode this relation is hidden behind a knows(a, b) call and the grade depends on making O(n) of them. The matrix here holds exactly the same information; reading all n² entries still solves it, but the intended answer touches only a linear number of them.

Example 1

011010102111012
row: they know nobodycolumn: everybody knows themthe diagonal, always 1 and ignored
Row a, column b is 1 when person a knows person b. A celebrity needs a row of all zeros off the diagonal and a column of all ones off it. Person 1 has exactly that pattern, so the answer is 1.
Everybody knows themselves, so the diagonal is 1 by definition and carries no information.
Input:  grid = [[1,1,0],[0,1,0],[1,1,1]]
Output: 1
Explanation: Person 1 knows only themselves, and both 0 and 2 know person 1.

Example 2

010111102011012
the diagonal, always 1 and ignored
No person in this matrix has both, so the answer is -1.
Input:  grid = [[1,0,1],[1,1,0],[0,1,1]]
Output: -1
Explanation: Everybody knows somebody else, so nobody qualifies.

Example 3

010
the diagonal, always 1 and ignored
Input:  grid = [[1]]
Output: 0
Explanation: One person, with nobody else to know, is vacuously a celebrity.

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 100
  • grid[i][j] is either 0 or 1
  • grid[i][i] == 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

Checking every person against everybody else is n² questions, and the structure of the relation makes most of them pointless.

The useful observation is that a single question eliminates a whole person. Ask whether a knows b. If yes then a knows somebody else, so a is not the celebrity. If no, then b is not known by everybody, so b is not the celebrity. Either way one candidate is gone for good.

So a single pass that keeps one surviving candidate narrows n people down to one in n-1 questions. That survivor is the only person who can possibly be the celebrity — but it has to be confirmed, because a party with no celebrity still leaves a survivor.

Approach

  1. Hold a candidate, starting at person 0.
  2. For each other person i: if the candidate knows i, the candidate is disqualified, so make i the new candidate. Otherwise i is disqualified and the candidate stands.
  3. After the pass, verify the survivor properly: they must know nobody else, and everybody else must know them.
  4. Return the survivor if both checks hold, and -1 otherwise.

Walkthrough

grid = [[1,1,0],
        [0,1,0],
        [1,1,1]]

candidate = 0
  i=1: grid[0][1] = 1  -> 0 knows 1, so 0 is out; candidate = 1
  i=2: grid[1][2] = 0  -> 1 does not know 2, so 2 is out; candidate stays 1

verify candidate 1
  knows nobody else:  grid[1][0]=0, grid[1][2]=0   ok
  known by everybody: grid[0][1]=1, grid[2][1]=1   ok

answer 1

Complexity

  • Time — O(n): n-1 questions to find the survivor and at most 2(n-1) to confirm them, so a linear number in total rather than n².
  • Space — O(1): one candidate index.

Common Pitfalls

  • Skipping the verification pass. The elimination always leaves a survivor, so without the check a party with no celebrity returns a confident wrong answer.
  • Letting the diagonal into the checks. grid[i][i] is 1 by definition, so a verification that includes the candidate against themselves rejects every genuine celebrity.
  • Re-reading the whole matrix during verification. Only the candidate’s row and column matter, which is what keeps the count linear.

Full Solution — all languages

class Solution:
    def findCelebrity(self, grid):
        n = len(grid)
        candidate = 0
        for i in range(1, n):
            if grid[candidate][i] == 1:
                candidate = i
        for i in range(n):
            if i == candidate:
                continue
            if grid[candidate][i] == 1 or grid[i][candidate] == 0:
                return -1
        return candidate
SensAI
Beta
Tap the mic to stop voice mode