Find the Town Judge

In a town of n people, the judge is trusted by everyone else and trusts nobody. Return the label of the judge, or -1 if no judge exists. Pattern focus: Graph Degree Analysis. The judge is the node with indegree n-1 and outdegree 0.

Input Format

n = number of people, trust = directed trust relationships [a, b]

Output Format

label of the judge, or -1 if none exists

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • n, trust must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

n = 2
trust = [[1,2]]

Output:

2

Explanation:

Person 2 is trusted by 1 and trusts nobody.

Example 2:

Input:

n = 3
trust = [[1,3],[2,3]]

Output:

3

Explanation:

Person 3 is trusted by everyone else.

Loading...
Find the Town Judge - Graph Fundamentals