Stacks and Queues Matching and cancelling
198. Find the Celebrity
There are n people at a party, numbered from 0 to n - 1. You are given an n × n table knows.
knows[a][b] is 1 if person a knows person b, and 0 if not. Knowing goes one way: a can know b
while b does not know a.
A celebrity is a person whom everyone else knows and who knows nobody else. Return the celebrity's number,
or -1 if there is no celebrity.
In the interview version of this problem, the table is hidden, and each look at it is a call to a function
knows(a, b). So try to look at as few entries as you can.
Example 1
Input: knows = [[1, 0, 1, 1, 0],
[1, 1, 0, 1, 1],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 1, 1]]
3Explanation: Persons 0, 1, 2 and 4 all know person 3, so column 3 is all ones. Row 3 has its only 1 on the diagonal, so person 3 knows nobody else.
Example 2
Input: knows = [[1, 1, 0], [1, 1, 0], [0, 1, 1]]
-1Explanation: Persons 0 and 2 both know person 1, but person 1 knows person 0, so person 1 is not a celebrity. Nobody else is known by everyone.
Example 3
Input: knows = [[1]]
0Explanation: Person 0 is alone. Nobody else is there to know them, and there is nobody else for them to know.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
n == knows.length == knows[i].length1 ≤ n ≤ 1000knows[i][j]is0or1knows[i][i] == 1: everyone knows themselves
Clarifying questions
What you would ask an interviewer before writing any code.