Find the Town Judge

Easy

Topics
GraphArrayHash Table

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

12
the judge
Arrows point from truster to trusted. The judge is trusted by everyone else and trusts nobody, so with trusted − trusts as a score the judge is the only node scoring n−1 = 1. That is node 2.
Edges are directed [a, b] meaning "a trusts b", over nodes 1..n.
Input:  n = 2, edges = [[1,2]]
Output: 2

Example 2

123
the judge
Input:  n = 3, edges = [[1,3],[2,3]]
Output: 3

Example 3

123
No node qualifies, so the answer is -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

  1. A score array of size n+1 (people are labelled from 1).
  2. For each pair (a, b): score[a] -= 1, score[b] += 1.
  3. 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 -> -1

Complexity

  • 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-1 people, without checking they trust nobody. The combined score enforces both at once, which is why it is worth using.
  • Sizing the array n instead of n+1. People are labelled 1..n.
  • Missing the n == 1 case, 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 -1
SensAI
Beta
Tap the mic to stop voice mode