277. Find the Celebrity
# The knows API is already defined for you.
# @param a, person a
# @param b, person b
# @return a boolean, whether a knows b
# def knows(a, b):
class Solution(object):
def findCelebrity(self, n):
"""
:type n: int
:rtype: int
"""
ix = 0
# check if someone knows ix and ix does not know anyone from ix + 1 to n -1
for i in range(n):
if knows(ix, i):
ix = i
if any(knows(ix, i) for i in range(ix)): return -1 # check if ix knows any one from 0 to ix -1
if any(not knows(i, ix) for i in range(n)): return -1 # check if everyone else knows ix
return ixLast updated
Was this helpful?