997. 找到小镇的法官

exiaohu 于 2021-12-19 发布

题目链接:997. 找到小镇的法官

from collections import Counter
from typing import List


class Solution:
    def findJudge(self, n: int, trust: List[List[int]]) -> int:
        if len(trust) == 0:
            return -1 if n > 1 else 1

        a, b = zip(*set(map(tuple, trust)))

        a = set(a)
        candidates = set(filter(lambda i: i not in a, range(1, n + 1)))

        if len(candidates) == 0:
            return -1

        bc, bcn = Counter(b).most_common(1)[0]
        if bcn >= n - 1 and bc in candidates:
            return bc

        return -1