Skip to main content

997. Find the Town Judge

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

Python

Prefix Sum (Two Arrays)

  • Use two arrays to store indegree and outdegree edges
class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
indegree = [0] * (n+1)
outdegree = [0] * (n+1)

for person, trusted in trust:
outdegree[person] += 1
indegree[trusted] += 1

for i in range(1, n+1):
if indegree[i] == n-1 and outdegree[i] == 0:
return i
return -1