Tap here to switch tabs
Problem
Submissions

Problem: Find the Celebrity

med
30 min
Try to solve the Find the Celebrity problem.

Statement

You are given n people labeled from 00 to n1n - 1. There is a relationship knows(a, b) indicating whether person a knows person b. The relationships are also represented by a matrix graph where graph[i][j] == 1 means person i knows person j, and graph[i][j] == 0 means they do not.

A person c is a celebrity if both conditions hold:

  1. For every person i != c, graph[i][c] == 1 (everyone knows c).

  2. For every person i != c, graph[c][i] == 0 (c knows nobody else).

Return the label of the celebrity if one exists, otherwise return 1-1.

Note: You should solve the problem using the knows API, and aim to keep the number of knows calls within 3n3 \cdot n.

Constraints:

  • n == graph.length == graph[i].length

  • 22 \leq n 100\leq 100

  • graph[i][j] is 00 or 11

  • graph[i][i] == 1

Tap here to switch tabs
Problem
Submissions

Problem: Find the Celebrity

med
30 min
Try to solve the Find the Celebrity problem.

Statement

You are given n people labeled from 00 to n1n - 1. There is a relationship knows(a, b) indicating whether person a knows person b. The relationships are also represented by a matrix graph where graph[i][j] == 1 means person i knows person j, and graph[i][j] == 0 means they do not.

A person c is a celebrity if both conditions hold:

  1. For every person i != c, graph[i][c] == 1 (everyone knows c).

  2. For every person i != c, graph[c][i] == 0 (c knows nobody else).

Return the label of the celebrity if one exists, otherwise return 1-1.

Note: You should solve the problem using the knows API, and aim to keep the number of knows calls within 3n3 \cdot n.

Constraints:

  • n == graph.length == graph[i].length

  • 22 \leq n 100\leq 100

  • graph[i][j] is 00 or 11

  • graph[i][i] == 1