bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 277. Find the Celebrity

277. Find the Celebrity

Leetcode Solutions

Logical Deduction with Single Pass

  1. Initialize celebrityCandidate to 0, assuming the first person might be the celebrity.
  2. Iterate over the range from 1 to n-1.
  3. For each person i, check if celebrityCandidate knows i.
    • If true, set celebrityCandidate to i because the current celebrityCandidate cannot be the celebrity.
    • If false, continue because i cannot be the celebrity.
  4. After the loop, celebrityCandidate is the only potential celebrity.
  5. Verify that celebrityCandidate is known by everyone and knows no one else.
  6. If verification passes, return celebrityCandidate. Otherwise, return -1.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...