Skip to content
Decoder

Find the Town Judge

Easy

Problem

A town has n people labelled 1 to n. Each pair [a, b] in trust means person a trusts person b. The town judge, if there is one, trusts nobody and is trusted by everyone else, and at most one person can fit that description. Return the judge's label, or -1 if there is no judge.

Example 1

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

Example 2

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

Example 3

Input
n = 3, trust = [[1, 3], [2, 3], [3, 1]]
Output
-1
Why
3 trusts someone

Constraints

  • 1 <= n <= 1000
  • 0 <= trust.length <= 10^4
  • a != b, and no pair appears twice

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.