Find the Town Judge

Easy

Topics
GraphArrayHash Table

In a town of n people labeled 1..n, exactly one person may be the town judge. Each edge [a, b] means person a trusts person b. The judge trusts nobody, and every other person trusts the judge. Return the judge's label, or -1 if there is no such person.

Example 1

Input:  n = 2, edges = [[1,2]]
Output: 2

Example 2

Input:  n = 3, edges = [[1,3],[2,3]]
Output: 3

Example 3

Input:  n = 3, edges = [[1,3],[2,3],[3,1]]
Output: -1

Constraints

  • 1 <= n <= 1000
  • edges[i] = [a, b] with a != b.
Run ⌘' · Submit ⌘⏎