文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Version 1
class Solution:def findJudge(self, N, trust):if N == 1:return 1if len(trust) < N - 1:return -1judge = {
}people = {
}for pair in trust:people[pair[0]] = people.get(pair[0], 0) + 1judge[pair[1]] = judge.get(pair[1], 0) + 1for key, value in judge.items():if value == N - 1 and key not in people:return keyreturn -1
- Version 2
class Solution:def findJudge(self, N, trust):count = [0] * (N + 1)for pair in trust:count[pair[0]] -= 1count[pair[1]] += 1for i in range(1, len(count)):if count[i] == N - 1:return ireturn -1
Reference
- https://leetcode.com/problems/find-the-town-judge/submissions/