← 返回 linkedin 的题目列表Find the Celebrity with Minimum API Calls
类型:online_judge
Problem: Find the Celebrity
There are n people labeled from 0 to n - 1.
A celebrity is defined as someone who:
Is known by everyone else;
Knows no one else.
You are given an API:
knows(a, b) -> bool
which returns whether person a knows person b.
Implement:
findCelebrity(n) -> int
Return the celebrity's label, or -1 if no celebrity exists.
Minimize the number of calls to the knows API.
Constraints
1 <= n <= 10^4
Calling knows(a, b) is expensive, so the number of API calls should be minimized.
Local Testing Format
For testing, the input provides an n x n matrix:
matrix[a][b] = 1 means a knows b
matrix[a][b] = 0 means a does not know b
Output the celebrity index or -1.
Example
Input
3
0 1 1
0 0 0
0 1 0
Output
1