Python using set


  • 0
    W
    def findCelebrity(self, n):
        candidates = [i for i in range(n)]
        while len(candidates)>1:
            a, b = candidates.pop(), candidates.pop()
            if knows(a, b) and not knows(b,a):
                candidates.append(b)
            elif not knows(a,b) and knows(b,a):
                candidates.append(a)
        if not candidates: return -1
        for i in range(n):
            if i != candidates[0] and (not knows(i, candidates[0]) or knows(candidates[0], i)): return -1
        return candidates[0]

Log in to reply
 

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