# 997. Find the Town Judge

• Time: $O(|\texttt{trust}| + n)$
• Space: $O(n)$
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public: int findJudge(int n, vector>& trust) { vector count(n + 1); for (vector& t : trust) { --count[t[0]]; ++count[t[1]]; } for (int i = 1; i < n + 1; ++i) if (count[i] == n - 1) return i; return -1; } }; 
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int findJudge(int n, int[][] trust) { int[] count = new int[n + 1]; for (int[] t : trust) { --count[t[0]]; ++count[t[1]]; } for (int i = 1; i < n + 1; ++i) if (count[i] == n - 1) return i; return -1; } } 
  1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution: def findJudge(self, n: int, trust: List[List[int]]) -> int: count = [0] * (n + 1) for a, b in trust: count[a] -= 1 count[b] += 1 for i in range(1, n + 1): if count[i] == n - 1: return i return -1