Menu
CoddyTech

The Celebrity Problem

There are n people at a party, numbered from 0 to n-1. You get an n × n matrix where matrix[i][j] is 1 if person i knows person j and 0 if not. Knowing is one-way: i can know j while j does not know i.

A celebrity is a person whom every other person knows and who knows no one. Return the celebrity's number, or -1 if there is no celebrity.

Function

findCelebrity(matrix: integer-2d-array) → integer
matrixinteger-2d-array
who knows whom: row i, column j is 1 if person i knows person j
Returnsinteger
the celebrity's number, or -1 if there is none

Constraints

  • 2 ≤ n ≤ 150, where n is the number of rows and of columns
  • matrix[i][j] is 0 or 1
  • matrix[i][i] = 0 for every person

Examples

Input
matrix = [[0, 1, 0], [0, 0, 0], [1, 1, 0]]
Output
1
Explanation
Row 1 is all zeros, so person 1 knows no one. Column 1 has a 1 in rows 0 and 2, so both other people know person 1. Person 1 is the celebrity.
challenge icon

Follow-up

Suppose each lookup is a question you must ask. How few questions do you need in the worst case, and can you get below 3(n-1)?

Reset code
def findCelebrity(matrix):
    # Write code here

Test #1

Test #2

Test #3

Argument

matrix = [[0, 1, 0], [0, 0, 0], [1, 1, 0]]

Expected Output

1