classSolution: deffindJudge(self, N, trust): if N == 1: return1 if len(trust) < N - 1: return-1 judge = {} people = {} for pair in trust: people[pair[0]] = people.get(pair[0], 0) + 1 judge[pair[1]] = judge.get(pair[1], 0) + 1
for key, value in judge.items(): if value == N - 1and key notin people: return key
return-1
Version 2
1 2 3 4 5 6 7 8 9 10 11
classSolution: deffindJudge(self, N, trust): count = [0] * (N + 1) for pair in trust: count[pair[0]] -= 1 count[pair[1]] += 1 for i in range(1, len(count)): if count[i] == N - 1: return i return-1