Identify the person who is trusted by everyone else but trusts nobody.
In a town with n people labeled from 1 to n, some pairs describe trust relationships. Each pair [a, b] means that person a trusts person b.
A town judge is a person who satisfies both conditions:
Return the label of the judge if one exists. Otherwise, return -1.
n: integer, the number of people labeled 1..ntrust: list of pairs [a, b] representing that a trusts b-1 if no such person exists.1 <= ntrust.length may be 01..nExample 1
Input
n = 2 trust = [[1,2]]
Output
2
Explanation
Person 2 is trusted by person 1 and trusts nobody, so person 2 is the judge.
Example 2
Input
n = 3 trust = [[1,3],[2,3]]
Output
3
Explanation
Person 3 is trusted by everyone else and trusts no one.
Example 3
Input
n = 3 trust = [[1,3],[2,3],[3,1]]
Output
-1
Explanation
Person 3 trusts person 1, so person 3 cannot be the judge.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.