Asked at
Find the Town Judge
EasyVerifiedGraphArrayHash Map~15 min
In a town of n people, exactly one may be the judge. The judge trusts nobody, and everybody else (the other n - 1 people) trusts the judge. Each trust[i] = [a, b] is a directed edge: person a trusts person b.
Return the judge’s label, or -1 if there is no judge. The input arrives as a single object { n, trust }.
Examples
in{ n: 3, trust: [[1,3],[2,3]] }
out3
Everyone trusts 3 and 3 trusts no one.
in{ n: 3, trust: [[1,3],[2,3],[3,1]] }
out-1
Candidate 3 trusts someone, so no judge exists.
Constraints
- 1 ≤ n ≤ 1000
- Directed pairs: trust[i] = [a, b] means a trusts b
- People are labelled 1..n.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.