Find the Town Judge — Solution in Python, Java & C++
Find the Town Judge solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Find the Town Judge is an easy coding problem on hash table and graph.
- Difficulty: Easy
- Topics: Hash Table, Graph
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
In a town of n people (labelled 1..n), the town judge trusts nobody and is trusted by everyone else. trust[i] = [a, b] means person a trusts person b.
Return the judge's label, or -1 if no judge exists.
Example 1
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Explanation: Person 3 trusts someone.
Constraints
1 <= n <= 150 <= trust.length <= 60All trust pairs are distinct, a != b.
Hints for Find the Town Judge
Hint 1
Think in-degree minus out-degree.
Hint 2
The judge scores exactly n-1: trusted by all, trusting none.
How to solve Find the Town Judge
The judge is defined purely by degrees: in-degree n - 1 (everyone trusts them) and out-degree 0 (they trust nobody). Combine the two into a single score — +1 for being trusted, -1 for trusting — and the judge is the unique person scoring exactly n - 1. No graph traversal needed.
Approach
- Keep a score per person, all starting at
0. - For each pair
[a, b]: decrementscore[a](a trusts someone) and incrementscore[b](b is trusted). - Scan people
1..nfor one whose score equalsn - 1. - Return that label, or
-1if nobody qualifies.
Why it works
Score is in-degree minus out-degree, which is at most n - 1 for anyone: in-degree cannot exceed n - 1 (no self-trust) and out-degree is non-negative. Hitting the maximum therefore forces in-degree to be exactly n - 1 and out-degree exactly 0 — precisely the judge's definition. That also proves uniqueness: two people both trusted by everyone else would have to trust each other, contradicting out-degree 0.
Complexity
- Time —
O(n + m) - Space —
O(n)
Pitfalls
- Size the score array
n + 1— people are labelled from1, not0. - The single combined score is what makes this work; checking in-degree alone wrongly accepts someone who is trusted by all and trusts someone.
n = 1with no trust pairs is a valid town: person 1 scores0 == n - 1and is the judge.- An empty trust list is allowed, so guard the scan rather than assuming an answer exists.
Reference solution
Python
from typing import List
def findJudge(n: int, trust: List[List[int]]) -> int:
score = [0] * (n + 1)
for a, b in trust:
score[a] -= 1
score[b] += 1
for i in range(1, n + 1):
if score[i] == n - 1:
return i
return -1Java
public static int findJudge(int n, int[][] trust) {
int[] score = new int[n + 1];
for (int[] t : trust) {
score[t[0]]--;
score[t[1]]++;
}
for (int i = 1; i <= n; i++) {
if (score[i] == n - 1) return i;
}
return -1;
}C++
int findJudge(int n, vector<vector<int>>& trust) {
vector<int> score(n + 1, 0);
for (const auto& t : trust) {
score[t[0]]--;
score[t[1]]++;
}
for (int i = 1; i <= n; i++) {
if (score[i] == n - 1) return i;
}
return -1;
}JavaScript
var findJudge = function(n, trust) {
const score = new Array(n + 1).fill(0);
for (const t of trust) {
score[t[0]]--;
score[t[1]]++;
}
for (let i = 1; i <= n; i++) {
if (score[i] === n - 1) return i;
}
return -1;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 439 hash table problems · the whole catalogue
Learn the technique: Hashing: Hash Maps and Hash Sets · Graph Data Structure