997. Find the Town Judge
Input: N = 2, trust = [[1,2]]
Output: 2Input: N = 3, trust = [[1,3],[2,3]]
Output: 3Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1Input: N = 3, trust = [[1,2],[2,3]]
Output: -1Last updated
Input: N = 2, trust = [[1,2]]
Output: 2Input: N = 3, trust = [[1,3],[2,3]]
Output: 3Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1Input: N = 3, trust = [[1,2],[2,3]]
Output: -1Last updated
Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3// Directed graph with counting degrees
int findJudge(int N, vector<vector<int>>& trust) { // time: O(T + N); space: O(N)
vector<int> degree(N + 1, 0);
for (vector<int>& t : trust) {
--degree[t[0]], ++degree[t[1]];
}
for (int i = 1; i <= N; ++i) {
if (degree[i] == N - 1) return i;
}
return -1;
}