Problem
In a town of n people labeled 1 through n, some people trust others. Given a list of [a, b] pairs meaning "person a trusts person b," find the town judge: the one person who trusts nobody but is trusted by everyone else. Return the judge's label, or -1 if no judge exists.
- Input:
n = 4,trust = [[1,3],[1,4],[2,3],[2,4],[4,3]] - Output:
3 - Explanation: Person 3 is trusted by persons 1, 2, and 4, and trusts no one themselves.
Counter-example: n = 3, trust = [[1,3],[2,3],[3,1]] โ -1, because person 3 trusts person 1, disqualifying them as judge even though two people trust them.
Intuition
Model trust as directed edges: a โ b means "a trusts b." The judge is the unique node with in-degree exactly n-1 (every other person points to them) and out-degree exactly 0 (they point to no one). A single pass over the trust list to count these degrees is all you need โ no traversal required.
Solution โ In/Out Degree Counting
Track two values per person: how many trust them (in-degree) and how many they trust (out-degree). The judge is the only person with in-degree n-1 and out-degree 0.
- Initialize
in_degreeandout_degreearrays of sizen+1to zero (1-indexed to match person labels). - For each pair
(truster, trusted), incrementout_degree[truster]andin_degree[trusted]. - Scan persons 1 to
n; return the first without_degree == 0andin_degree == n-1. - Return
-1if no such person exists.
1def findJudge(n: int, trust: list[list[int]]) -> int:
2 in_degree = [0] * (n + 1)
3 out_degree = [0] * (n + 1)
4
5 for truster, trusted in trust:
6 out_degree[truster] += 1 # this person gives trust to someone
7 in_degree[trusted] += 1 # this person receives trust
8
9 for person in range(1, n + 1):
10 # judge trusts nobody and is trusted by everyone else
11 if out_degree[person] == 0 and in_degree[person] == n - 1:
12 return person
13
14 return -1Time: O(n + t) โ one pass through t trust pairs to build degrees, one pass through n people to find the judge.
Space: O(n) โ two arrays of size n+1 for in- and out-degrees.
Complexity Summary
| Approach | Time | Space | When to use |
|---|---|---|---|
| In/Out Degree Counting | O(n + t) | O(n) | Always โ single pass, minimal bookkeeping |
Common Mistakes
- Checking only in-degree: A person trusted by
n-1others could still trust someone โ you must also verifyout_degree == 0or the answer is wrong. The counter-example above illustrates this failure mode. - Off-by-one on the in-degree threshold: The judge must be trusted by exactly
n-1people (everyone except themselves), notn. Checking>= n-1would incorrectly pass someone trusted by allnif that were possible. - 0-indexed arrays with 1-indexed people: Allocating size
nand indexing by person label collides personnwith indexn, corrupting its count. Always allocate sizen+1. - Forgetting to return
-1: The problem guarantees at most one judge, but not that one always exists. A missing return at the end causes undefined behavior or a wrong answer. - Assuming
n=1is a special case: Ifn=1andtrust=[], the algorithm correctly returns1โ in-degree is0 = n-1and out-degree is0, so person 1 is vacuously the judge with no one else in town.
Related Problems
course-scheduleโ directed graph where you detect cycles among dependency edges, a different use of in/out degree reasoningnumber-of-provincesโ finding how many disconnected components exist in an undirected trust-like graphfind-if-path-exists-in-graphโ basic graph reachability via BFS or DFS over the same edge-list formatevaluate-divisionโ graph edges carry numeric weights; finding a value requires traversing paths rather than counting degreesall-paths-from-source-to-targetโ enumerate all paths in a directed acyclic graph using DFS with backtracking