Home LeetCode. 997. Find the Town Judge
Post
Cancel

LeetCode. 997. Find the Town Judge

image

[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;
    }
}
This post is licensed under CC BY 4.0 by the author.