InferenceAI

Enter at least two characters to search.

Start free

Find the Celebrity

mediumStack25 min25 XP

knows[i][j] is 1 if person i knows person j. A celebrity is known by everyone else and knows nobody else. Return the celebrity's index, or -1 if there is none.

Requirements

  • O(n)O(n) questions: eliminate one candidate per comparison.

Examples

EXAMPLE WALKTHROUGHperson 1

A guided view of the published example, not a trace of your code.

INPUT[[[0,1,0],[0,0,0],[0,1,0]]]
TASKfind_the_celebrity()
EXPECTED OUTPUT1

Start with the input values shown in this example.

Example 1 of 3

Input: knows = [[0,1,0],[0,0,0],[0,1,0]]

Output: 1

Explanation: person 1

Input: knows = [[0,1],[1,0]]

Output: -1

Explanation: no celebrity

Input: knows = [[0]]

Output: 0

Explanation: alone

Hint 1

If a knows b, a can't be the celebrity; otherwise b can't be.

Hint 2

After one pass, verify the single remaining candidate.

Topics

DSAStackPython