Skip to content

Find the Town Judge

EasyGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

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.

n = 2, trust = [[1, 2]]                  →  2
n = 3, trust = [[1, 3], [2, 3]]          →  3
n = 3, trust = [[1, 3], [2, 3], [3, 1]]  →  -1   (3 trusts someone)

Constraints

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