Find the Celebrity
Medium
Topics
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
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.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
Input: grid = [[1,0,1],[1,1,0],[0,1,1]] Output: -1 Explanation: Everybody knows somebody else, so nobody qualifies.
Example 3
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
- Hold a candidate, starting at person 0.
- For each other person
i: if the candidate knowsi, the candidate is disqualified, so makeithe new candidate. Otherwiseiis disqualified and the candidate stands. - After the pass, verify the survivor properly: they must know nobody else, and everybody else must know them.
- Return the survivor if both checks hold, and
-1otherwise.
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 1Complexity
- 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 candidateimport java.util.*;
class Solution {
public int findCelebrity(int[][] grid) {
int n = grid.length;
int candidate = 0;
for (int i = 1; i < n; i++) if (grid[candidate][i] == 1) candidate = i;
for (int i = 0; i < n; i++) {
if (i == candidate) continue;
if (grid[candidate][i] == 1 || grid[i][candidate] == 0) return -1;
}
return candidate;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findCelebrity(vector<vector<int>>& grid) {
int n = grid.size();
int candidate = 0;
for (int i = 1; i < n; i++) if (grid[candidate][i] == 1) candidate = i;
for (int i = 0; i < n; i++) {
if (i == candidate) continue;
if (grid[candidate][i] == 1 || grid[i][candidate] == 0) return -1;
}
return candidate;
}
};/**
* @param {number[][]} grid
* @return {number}
*/ var findCelebrity = function(grid) {
const n = grid.length;
let candidate = 0;
for (let i = 1; i < n; i++) if (grid[candidate][i] === 1) candidate = i;
for (let i = 0; i < n; i++) {
if (i === candidate) continue;
if (grid[candidate][i] === 1 || grid[i][candidate] === 0) return -1;
}
return candidate;
};