Find the Celebrity

Pattern: Implementation

medium

A celebrity knows nobody and everyone knows them. Compare a candidate with each person: if the candidate knows i, the candidate is out and i becomes the candidate; otherwise i is out. One survivor remains, and a final pass checks them.

The problem

There are n people, 0 to n - 1. knows[a][b] is 1 when a knows b (and knows[i][i] is 0). A celebrity is known by everyone else and knows no one. Return the celebrity's number, or -1 if there is none.

LeetCode's version (premium) hides the matrix behind a knows(a, b) function and asks you to call it as few times as possible; this guide passes the matrix itself. The method is the same.

Think first

  • What does one question, does a know b, tell you?
  • How many people can be ruled out per question?
  • Why is a final check still needed?

Hints

    Brute force, and why it fails

    Check every person against every other: O(n²). A stack version pushes everyone and pops two at a time, keeping one, which is the same elimination with more memory.

    The key insight

    Every question eliminates someone, so one pass narrows n people to one candidate. The candidate must then be verified in O(n).

    Dry run

    knows = [[0,1,0],[0,0,0],[0,1,0]]. Start with candidate 0.

    Step 1 of 4

    The plan

    candidate = 0
    for i from 1 to n - 1:
        if knows[candidate][i]: candidate = i
    for i != candidate:
        if knows[candidate][i] or not knows[i][candidate]: return -1
    return candidate
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Two passes over the people, each asking one or two questions per person.

    Edge cases

    • One person: they are a celebrity.
    • Two people who know each other: none.
    • The celebrity is person 0 or person n - 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int findCelebrity(int[][] knows) {
            int n = knows.length, candidate = 0;
            for (int i = 1; i < n; i++) {
                if (knows[candidate][i] == 1) candidate = i;   // candidate knows i: candidate is out
            }                                                   // otherwise i is out (nobody ignores a celebrity)
            for (int i = 0; i < n; i++) {           // check the one survivor
                if (i == candidate) continue;
                if (knows[candidate][i] == 1 || knows[i][candidate] == 0) return -1;
            }
            return candidate;
        }
    }

    C++

    class Solution {
    public:
        int findCelebrity(vector<vector<int>>& knows) {
            int n = static_cast<int>(knows.size()), candidate = 0;
            for (int i = 1; i < n; i++) {
                if (knows[candidate][i] == 1) candidate = i;   // candidate knows i: candidate is out
            }                                                   // otherwise i is out (nobody ignores a celebrity)
            for (int i = 0; i < n; i++) {           // check the one survivor
                if (i == candidate) continue;
                if (knows[candidate][i] == 1 || knows[i][candidate] == 0) return -1;
            }
            return candidate;
        }
    };

    Python

    class Solution:
        def findCelebrity(self, knows: list[list[int]]) -> int:
            n, candidate = len(knows), 0
            for i in range(1, n):
                if knows[candidate][i] == 1:
                    candidate = i                   # candidate knows i: candidate is out (otherwise i is out)
            for i in range(n):                      # check the one survivor
                if i != candidate and (knows[candidate][i] == 1 or knows[i][candidate] == 0):
                    return -1
            return candidate

    Common mistakes

    • Skipping the final check.
    • In the check, testing knows[candidate][candidate].

    Similar problems