Python Fast and Easy Solution


  • 0
    D
    # 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
            """
            def query(candidates):
                if not candidates: return -1
                p = candidates[0]
                if len(candidates) == 1: return p
                m = candidates[1]
                if knows(p, m): 
                    del candidates[0]
                else:
                    del candidates[1]
                return query(candidates)
                
                
            p = query(list(range(n)))
            if p == -1: return p
            if all(knows(x, p) for x in list(range(n))) and not any(knows(p, x) and p!=x for x in list(range(n))): return p
            return -1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.