Suppose you are at a party with n
people (labeled from 0
to n - 1
) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1
people know him/her but he/she does not know any of them.
Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hey, A, do you know B?" to get information about whether A knows B. You need to find out the celebrity (if there is one) by asking as few questions as possible (in the asymptotic sense).
You are given a helper function bool knows(a, b)
which tells you whether A knows B. Implement a function int celebrity(n)
that returns the celebrity's label if the celebrity is present at the party. If there is no celebrity, return -1
.
Note: The relation is transitive. That is, if A knows B and B knows C, then A knows C.
You will not have direct access to the knowledge of relationships between people. The judge will use your implementation to assess your algorithm.
Example 1:
Input: graph = [[1,1,0],[0,1,0],[1,1,1]] Output: 1 Explanation: There are three people labeled 0, 1 and 2. graph[i][j] = 1 means person i knows person j, otherwise graph[i][j] = 0 means person i does not know person j. The celebrity is person 1 because everyone except him knows him and he knows no one.
Example 2:
Input: graph = [[1,0,1],[1,1,0],[0,1,1]] Output: -1 Explanation: There is no celebrity.
Constraints:
n == graph.length
n == graph[i].length
2 <= n <= 100
graph[i][j]
is 0
or 1
.graph[i][i] == 1
Follow up: Minimize the number of calls to knows
.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding a celebrity involves checking every single person to see if they fit the celebrity criteria. We essentially test each person's celebrity status individually by examining their relationships with everyone else. This means exhaustively comparing each person against all other people to see if they are known by everyone and know no one else.
Here's how the algorithm would work step-by-step:
def find_celebrity_brute_force(number_of_people):
for potential_celebrity in range(number_of_people):
everyone_knows_potential_celebrity = True
potential_celebrity_knows_no_one = True
# Check if everyone knows the potential celebrity.
for other_person in range(number_of_people):
if other_person != potential_celebrity and not knows(other_person, potential_celebrity):
everyone_knows_potential_celebrity = False
break
# Check if the potential celebrity knows anyone.
if everyone_knows_potential_celebrity:
for other_person in range(number_of_people):
if other_person != potential_celebrity and knows(potential_celebrity, other_person):
potential_celebrity_knows_no_one = False
break
# If both conditions are true, we found a celebrity.
if potential_celebrity_knows_no_one:
return potential_celebrity
return -1
The problem is like finding a famous person in a group. We can efficiently identify this person by strategically eliminating candidates based on pairwise relationships, eventually narrowing down to a single potential celebrity. Then we confirm that person truly meets the celebrity criteria.
Here's how the algorithm would work step-by-step:
def find_celebrity(number_of_people):
potential_celebrity = 0
for i in range(1, number_of_people):
if knows(potential_celebrity, i):
#Eliminate potential celebrity, i is new candidate
potential_celebrity = i
# Verify the potential celebrity
for i in range(number_of_people):
# Check if anyone doesn't know potential celebrity
if i != potential_celebrity and not knows(i, potential_celebrity):
return -1
# Check if the potential celebrity knows anyone
if i != potential_celebrity and knows(potential_celebrity, i):
return -1
return potential_celebrity
def knows(person_a, person_b):
#Placeholder function, replace with actual implementation
pass
Case | How to Handle |
---|---|
Null or empty knows matrix (n=0) | Return -1 immediately as there are no people to check. |
Single person (n=1) | If knows(0, 0) is false, return 0, otherwise return -1. |
No celebrity exists | The algorithm should correctly identify that no person satisfies the celebrity criteria and return -1. |
All people know each other | No one will satisfy the condition of not knowing anyone, so the algorithm should return -1. |
One person knows everyone else | No one will satisfy the condition of being known by everyone, so the algorithm should return -1. |
Large value of n (scalability) | Ensure the solution's time complexity is O(n) to handle large inputs efficiently, avoiding nested loops. |
Integer overflow when calculating indices (if using bit manipulation) | Use appropriate data types and avoid operations that may lead to overflow, checking the problem constraints for maximum input sizes. |
A cycle exists in the 'knows' relationship (e.g., A knows B, B knows C, C knows A) | The algorithm should still correctly identify if a celebrity exists despite the cycle, or return -1 if none exists as the celebrity must be known by everyone, and not know anyone. |