class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
if n == 1 and not trust:
return 1
trust_scores = [0] * (n + 1)
for a, b in trust:
trust_scores[a] -= 1
trust_scores[b] += 1
for i in range(1, n + 1):
if trust_scores[i] == n - 1:
return i
return -1
class Solution {
public:
int findJudge(int n, vector<vector<int>>& trust) {
if (n == 1 && trust.empty()) return 1;
vector<int> trustScores(n + 1, 0);
for (auto &t : trust) {
trustScores[t[0]]--;
trustScores[t[1]]++;
}
for (int i = 1; i <= n; ++i) {
if (trustScores[i] == n - 1) return i;
}
return -1;
}
};
class Solution {
public int findJudge(int n, int[][] trust) {
if (n == 1 && trust.length == 0) return 1;
int[] trustScores = new int[n + 1];
for (int[] t : trust) {
trustScores[t[0]]--;
trustScores[t[1]]++;
}
for (int i = 1; i <= n; i++) {
if (trustScores[i] == n - 1) return i;
}
return -1;
}
}
var findJudge = function(n, trust) {
if (n === 1 && trust.length === 0) return 1;
const trustScores = new Array(n + 1).fill(0);
for (const [a, b] of trust) {
trustScores[a]--;
trustScores[b]++;
}
for (let i = 1; i <= n; i++) {
if (trustScores[i] === n - 1) return i;
}
return -1;
};
You are given an integer n
representing the number of people in a town labeled from 1
to n
. There is a list trust
where each element is a pair [a, b]
meaning person a
trusts person b
.
The "town judge" is a person who:
n-1
people)
Your task is to find the label of the town judge, or return -1
if the judge does not exist. There is at most one valid judge.
To solve this problem, let's break down the requirements:
n-1
.
A brute-force approach would check each person to see if they are trusted by everyone else and do not trust anyone, but this would be inefficient. Instead, we can use a single array to track both "trusts" and "trusted by" counts for each person, updating as we process the trust
list. This optimization allows us to efficiently find the judge in a single pass.
Let's outline the steps to solve the problem efficiently:
trustScores
of size n+1
(since people are labeled from 1 to n).[a, b]
in trust
:trustScores[a]
by 1 (since a
trusts someone, disqualifying them from being judge).trustScores[b]
by 1 (since b
is trusted by someone).n-1
(trusted by everyone else, trusts nobody).trustScores
array for a person with a value of n-1
.-1
.n == 1
and trust
is empty, the only person is the judge by default.
Let's consider n = 3
and trust = [[1,3], [2,3]]
.
trustScores = [0, 0, 0, 0]
(index 0 unused).[1,3]
:
trustScores[1] -= 1
→ trustScores = [0, -1, 0, 0]
trustScores[3] += 1
→ trustScores = [0, -1, 0, 1]
[2,3]
:
trustScores[2] -= 1
→ trustScores = [0, -1, -1, 1]
trustScores[3] += 1
→ trustScores = [0, -1, -1, 2]
trustScores[i] == n-1 == 2
:
trustScores[1] = -1
(not judge)trustScores[2] = -1
(not judge)trustScores[3] = 2
(judge found!)Brute-force approach:
The key insight is to track both "trusted by" and "trusts" counts in a single array, allowing us to identify the judge in one pass. By incrementing and decrementing trust scores, we efficiently filter out anyone who trusts someone else or isn't trusted by everyone. This elegant solution is simple, fast, and easy to implement in any language.