Leetcode 1039 Solution

This article provides solution to leetcode question 1039 (find-the-town-judge)

https://leetcode.com/problems/find-the-town-judge

Solution

class Solution: def findJudge(self, N: int, trust: List[List[int]]) -> int: m = collections.defaultdict(set) for s, d in trust: m[s].add(d)
judge = None for i in range(1, N + 1): if not m[i]: if not judge: judge = i else: return -1
for i in range(1, N + 1): if i == judge: continue if judge not in m[i]: return -1
return judge