Solution: Find the Celebrity
Let’s solve the Find the Celebrity problem.
We'll cover the following
Statement
In a gathering of individuals (labeled from to ), there’s a possibility of one person being a celebrity. A celebrity is characterized by being known by everyone else and not knowing any attendees. This scenario is represented using an binary , where each cell contains either a or a . If , it signifies that person knows the person.
For the given , determine the existence of a celebrity within the group. If a celebrity is identified, return its label, otherwise return .
Constraints:
- is or .
Solution
Before we delve into the solution, it’s crucial to consider the minimum and maximum number of celebrities that can exist within the given matrix.
To explore the possibility of having more than one celebrity, consider two individuals, and , both purported to be celebrities. Following the celebrity criteria:
- , being a celebrity, should not know , implying . Consequently, should know , or .
- Conversely, for to be a celebrity, should not know , implying , while must know , or .
These conditions contradict each other, as they suggest that should both know and not know simultaneously, which is impossible. Thus, the assumption of having more than one celebrity contradicts the definition of a celebrity in this context. Therefore, the maximum number of celebrities possible in a matrix is .
Now, let’s dive deep into the solution. To identify a celebrity at a party, this solution employs an approach utilizing a stack data structure and a binary matrix representation of individuals at the party.
Let’s walk through the steps of the solution:
-
We create a stack to hold the indexes of all attendees, treating everyone as a potential celebrity initially.
-
We populate the stack with every attendee’s index, starting from up to ().
-
We determine potential celebrities by repeatedly popping two indexes from the stack to compare two individuals at a time.
-
For the popped individuals, we check whether one knows the other by referencing the binary matrix, where a value of at indicates that person knows person .
- If individual knows individual , cannot be a celebrity; is considered a potential celebrity and is pushed back into the stack.
- Otherwise, does not know person , is eliminated from consideration, and is pushed back into the stack for further comparison.
-
Continue the process until only one index(individual) remains in the stack. This index represents the tentative celebrity.
-
-
Next, to verify the celebrity status, we ensure that the tentative celebrity does not know any of the other attendees (no outgoing acquaintance) while every other attendee knows them (incoming acquaintance from all).
- We check the binary matrix for the following conditions for the tentative celebrity index:
- The row corresponding to the tentative celebrity should have all zeros (except for the diagonal element), indicating they know no one. We do not need to check the diagonal element here.
- The column corresponding to the tentative celebrity should have all ones (except for the diagonal element), indicating everyone knows them. We do not need to check the diagonal element here.
- We check the binary matrix for the following conditions for the tentative celebrity index:
-
Finally, if the tentative celebrity meets the verification criteria, their index is returned as the confirmed celebrity. Otherwise, we return , signifying the absence of a celebrity.
This solution efficiently narrows down the list of potential celebrities and validates the final candidate to ensure they meet the strict criteria of being known by everyone without knowing anyone at the party.
Let’s look at the illustration below to better understand the solution:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.