Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Find the Town Judge

XPChallenge Points: 10
levelLevel: Easy

In a town of n people labeled from 1 to n, there is a rumor that one of them is the town judge.
The town judge must satisfy two conditions:
1. The judge trusts nobody.
2. Everybody else (except the judge) trusts the judge.
You are given an array trust where trust[i] = [a, b] means person a trusts person b.
Return the label of the town judge if one exists, otherwise return -1.
________________________________________
🧠 Approach
We can use in-degree and out-degree logic:
• If person a trusts person b:
o a has out-degree +1 (trusts someone)
o b has in-degree +1 (is trusted by someone)
The judge is the person who:
• Has in-degree = n - 1 (everyone trusts them)
• Has out-degree = 0 (trusts no one)

Example 1:

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

Output: 2

Example 2:

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

Output: 3

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result