[Link] https://leetcode.com/problems/find-the-town-judge/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public int findJudge(int n, int[][] trust) {
if(n == 1) return trust.length == 0 ? 1 : -1;
int[] trustedBy = new int[n + 1];
boolean[] cantBe = new boolean[n + 1];
for(int[] ar: trust) {
cantBe[ar[0]] = true;
trustedBy[ar[1]]++;
}
for(int i = n; i > 0; i--) if(!cantBe[i] && trustedBy[i] == n-1) return i;
return -1;
}
}