Find the Town Judge
Identify the town judge who trusts nobody and is trusted by everyone else using in-degree and out-degree counting.
01The problem
In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
- The town judge trusts nobody.
- Everybody (except for the town judge) trusts the town judge.
- There is exactly one person that satisfies properties 1 and 2.
You are given an array trust where trust[i] = [a_i, b_i] representing that the person labeled a_i trusts the person labeled b_i.
Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
| Input | Output | Why |
|---|---|---|
n = 3, trust = [[1, 3], [2, 3]] | 3 | Person 3 is trusted by 1 and 2, and trusts nobody. |
n = 3, trust = [[1, 3], [2, 3], [3, 1]] | -1 | Person 3 trusts 1, so 3 cannot be judge. |
Constraints
1 <= n <= 10000 <= trust.length <= 10^4trust[i].length == 2- All the pairs of
trustare unique. a_i != b_i1 <= a_i, b_i <= n
02How to spot it
- "Trusts nobody + trusted by all": In graph terms:
- Out-degree must be
0(trusts nobody). - In-degree must be
n - 1(trusted by everyone else).
- Out-degree must be
- Single score array: Maintain
score[p] = inDegree[p] - outDegree[p]. The judge must havescore[p] == n - 1!
03Pseudocode
score = array of n + 1 zeros
for each [a, b]:
score[a] -= 1 ; score[b] += 1
return p with score[p] == n - 1
(or -1)04Watch it work
trust score (1..n)
target score for judge
2
Find town judge among 3 people: score[p] = inDegree - outDegree. Judge must have score == 2.
Pseudocode
- score = array of n + 1 zeros
- for each [a, b]:
- score[a] -= 1 ; score[b] += 1
- return p with score[p] == n - 1
- (or -1)
Variables
- n
- 3
- trust
- 1,3,2,3
05Step-by-step dry run
We trace n = 3, trust = [[1, 3], [2, 3]]:
| Person | Initial score | After [1, 3] | After [2, 3] | Final score | Judge? (score == 2) |
|---|---|---|---|---|---|
1 | 0 | -1 | -1 | -1 | No |
2 | 0 | 0 | -1 | -1 | No |
3 | 0 | +1 | +2 | +2 | YES! (score == 3 - 1) |
Output: 3.
06Java solution
public class Solution {
public static int findJudge(int n, int[][] trust) {
int[] score = new int[n + 1];
for (int[] t : trust) {
score[t[0]]--; // out-degree
score[t[1]]++; // in-degree
}
for (int p = 1; p <= n; p++) {
if (score[p] == n - 1) {
return p;
}
}
return -1;
}
public static void main(String[] args) {
int[][] trust = {{1, 3}, {2, 3}};
System.out.println(findJudge(3, trust));
}
}3
07Why it works
The judge has n - 1 incoming edges and 0 outgoing edges, so inDegree - outDegree = (n - 1) - 0 = n - 1. Any non-judge either trusts someone (reducing their score by at least 1) or is not trusted by everyone (keeping their incoming count below n - 1), making a score of n - 1 uniquely achievable by the judge.
08Time and space
- Time complexity:
O(E + n)whereE = trust.length. One pass over trust edges, one pass overnpeople. - Space complexity:
O(n)to store the integer score array.
09Edge cases
n = 1andtrust = []: Person 1 has score 0 which equalsn - 1 = 0. Returns 1.- Judge trusts someone: Score drops below
n - 1; returns -1.
10Common mistakes
- Allocating two separate arrays for in-degree and out-degree: While correct, a single array
score[i] = in[i] - out[i]is cleaner and halves memory.
11In the interview
- Opening line: "The town judge is defined by graph degrees: an in-degree of
n - 1and an out-degree of 0. I will maintain a net score array wherescore[i] = inDegree[i] - outDegree[i]. The judge must have scoren - 1."
12Quiz
Why is it sufficient to check only that score[p] == n - 1?
- (Recommended) Because each person can have at most n - 1 incoming trust edges, so achieving n - 1 requires having 0 outgoing edges and n - 1 incoming edges
- Because only one person exists in town
- Because negative scores are disallowed
- Because town judges always have label n
Answer: Because each person can have at most n - 1 incoming trust edges, so achieving n - 1 requires having 0 outgoing edges and n - 1 incoming edges.
13Solve it yourself
Open LeetCode #997 and submit the degree score solution.