Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access