Find the Town Judge
Easy
Topics
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
trusted − trusts as a score the judge is the only node scoring n−1 = 1. That is node 2.[a, b] meaning "a trusts b", over nodes 1..n.Input: n = 2, edges = [[1,2]] Output: 2
Example 2
Input: n = 3, edges = [[1,3],[2,3]] Output: 3
Example 3
-1.Input: n = 3, edges = [[1,3],[2,3],[3,1]] Output: -1
Constraints
- 1 <= n <= 1000
- edges[i] = [a, b] with a != b.
Editorial — intuition, approach, complexity and pitfalls
Intuition
The judge is trusted by everyone else and trusts nobody. Rather than tracking two separate tallies, note that one number captures both conditions: trusted-by minus trusts. For the judge that is (n-1) - 0 = n-1, and nobody else can reach it — anyone who trusts at least one person loses a point, and only the judge can be trusted by all the others.
So keep one score per person: +1 for each person who trusts them, -1 for each person they trust. Whoever ends on n-1 is the judge.
This also handles the "no judge" case without a separate check: if nobody reaches n-1, there is none.
Approach
- A score array of size
n+1(people are labelled from 1). - For each pair
(a, b):score[a] -= 1,score[b] += 1. - Return the person with score
n-1, else -1.
Dry Run
n = 3, trust = [[1,3],[2,3]]
start score = [_, 0, 0, 0]
(1,3) score[1]-- score[3]++ [_, -1, 0, 1]
(2,3) score[2]-- score[3]++ [_, -1, -1, 2]
n-1 = 2, and person 3 has score 2 -> judge is 3
n = 3, trust = [[1,3],[2,3],[3,1]]
3 now trusts someone, so score[3] = 2-1 = 1 != 2
nobody reaches 2 -> -1Complexity
- Time — O(n + e): one pass over the trust pairs, one over the people.
- Space — O(n): the score array. Two separate in/out-degree arrays would work identically; the single score is just less to carry.
Common Pitfalls
- Checking only that someone is trusted by
n-1people, without checking they trust nobody. The combined score enforces both at once, which is why it is worth using. - Sizing the array
ninstead ofn+1. People are labelled1..n. - Missing the
n == 1case, where there are no trust pairs and person 1 is vacuously the judge with score 0 == n-1. The formula gets it right; an ad-hoc version usually does not. - Returning early inside the edge loop, before all pairs are counted.
Full Solution — all languages
class Solution:
def findJudge(self, n, edges):
score = [0] * (n + 1)
for a, b in edges:
score[a] -= 1
score[b] += 1
for i in range(1, n + 1):
if score[i] == n - 1:
return i
return -1import java.util.*;
class Solution {
public int findJudge(int n, int[][] edges) {
int[] score = new int[n + 1];
for (int[] e : edges) {
score[e[0]]--;
score[e[1]]++;
}
for (int i = 1; i <= n; i++) if (score[i] == n - 1) return i;
return -1;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findJudge(int n, vector<vector<int>>& edges) {
vector<int> score(n + 1, 0);
for (auto& e : edges) {
score[e[0]]--;
score[e[1]]++;
}
for (int i = 1; i <= n; i++) if (score[i] == n - 1) return i;
return -1;
}
};/**
* @param {number} n
* @param {number[][]} edges
* @return {number}
*/ var findJudge = function(n, edges) {
const score = new Array(n + 1).fill(0);
for (const [a, b] of edges) {
score[a]--;
score[b]++;
}
for (let i = 1; i <= n; i++) if (score[i] === n - 1) return i;
return -1;
};