Find the Town Judge

Try to solve the Find the Town Judge problem.

Statement

There are n people numbered from 11 to n in a town. There's a rumor that one of these people is secretly the town judge. If there is a town judge, they must meet these conditions:

There are n people numbered from 11 to n in a town. There’s a rumor that one of these people is secretly the town judge. A town judge must meet the following conditions:

  • The judge doesn’t trust anyone.

  • Everyone else (except the judge) trusts the judge.

  • Only one person in the town can meet both of these conditions.

Given there is n and a two-dimensional array called trust,  each element trust[i] has two elements [ai,bi][a_i,b_i], it means that a trusts person b. If there is a judge, return their number, return −1-1 otherwise.

Constraints:

  • 1≤1 \leqn ≤1000\leq 1000

  • 0≤0 \leqtrust.length ≤104\leq 10^{4}

  • trust[i].length ==2 == 2

  • ai ≠\ne bi

  • 1≤1 \leqai , bi ≤\leqn

  • All the pairs in trust are unique.

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.