Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions findTheCelebrity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The knows API is already defined for you.
# Returns a boolean, indicating whether person a knows person b.
# def knows(a: int, b: int) -> bool:

class Solution:
# TC : O(n)
# SC : O(1)
def findCelebrity(self, n: int) -> int:

candidate = 0

for i in range(1, n):
# If the candidate knows person i, then switch candidate to i
if knows(candidate, i):
candidate = i

# Verify candidate is indeed a celebrity
for i in range(n):
if candidate != i:
# Candidate should not know anyone, and everyone should know the candidate
if knows(candidate, i) or not knows(i, candidate):
# If the condition fails, return -1 because there is no celebrity
return -1

# If all checks pass, return the candidate as the celebrity
return candidate