Find the Town Judge (LeetCode 997): Solution and Explanation

Easy
Array
Hash Table
Graph

A town has n people labeled 1 to n. The judge, if one exists, trusts nobody and is trusted by everybody else. Given the list of trust pairs, return the judge's label or -1.

Intuition

Draw each person as a node and each pair [a, b] as an arrow from a to b. The judge is the one node with no outgoing arrows and an incoming arrow from every other node.

That is a statement about degrees: out-degree 0 and in-degree n - 1. You could count both, but one number is enough.

Give every person a score of in-degree minus out-degree. The largest in-degree possible is n - 1, so a score of n - 1 forces in-degree n - 1 and out-degree 0 at the same time. Any person who trusts even one other person falls short.

Since only one person can be trusted by all others while trusting nobody, the first person whose score is n - 1 is the answer.

Approach: Net trust score

  1. Create score of size n + 1 filled with 0 (index 0 unused).
  2. For each pair [a, b], subtract 1 from score[a] because a trusts someone, and add 1 to score[b] because b is trusted.
  3. Scan people 1 to n and return the first person whose score equals n - 1.
  4. If nobody reaches n - 1, return -1.

Step-by-step example

n = 2, trust = [[1,2]]
  1. Scores start at [_, 0, 0] for persons 1 and 2.
  2. Pair [1, 2]: person 1 trusts someone, score[1] = -1. Person 2 is trusted, score[2] = 1.
  3. The judge needs a score of n - 1 = 1.
  4. score[1] = -1 is not 1, so person 1 is not the judge.
  5. score[2] = 1, so return 2.

Code: Python, Java, C++

1class Solution:
2    def findJudge(self, n: int, trust: List[List[int]]) -> int:
3        score = [0] * (n + 1)
4        for a, b in trust:
5            score[a] -= 1
6            score[b] += 1
7        for person in range(1, n + 1):
8            if score[person] == n - 1:
9                return person
10        return -1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n + t)

One pass over the t trust pairs and one pass over the n people.

Space

O(n)

One integer score per person.

Other approaches

Separate in-degree and out-degree arrays (time O(n + t), space O(n))

Count incoming and outgoing trust in two arrays and look for the person with in-degree n - 1 and out-degree 0. Same idea and cost, just one more array.

Check every candidate (time O(n * t), space O(1))

For each person, scan the whole trust list to see whether they trust anyone and who trusts them. Simple but repeats the scan n times.

Common mistakes

  • Checking only in-degree. A person trusted by everyone who also trusts someone back is not the judge, as Example 3 shows.
  • Forgetting n = 1 with no pairs. The lone person trusts nobody and is trusted by all 0 others, so the answer is 1, and the score check 0 == n - 1 handles it.
  • Using a 0-indexed array of size n and writing to score[n]. People are labeled 1 to n, so allocate n + 1 slots.
  • Returning the person with the highest score instead of exactly n - 1. A high score below n - 1 still means someone does not trust them.

Frequently asked questions

Why is a single score enough?

Score is in-degree minus out-degree, and in-degree is at most n - 1. Reaching n - 1 therefore requires in-degree n - 1 and out-degree 0 together, which is exactly the judge's definition.

Can there be two judges?

No. If two people both had in-degree n - 1, each would trust the other, giving both an out-degree of at least 1. So at most one person can score n - 1.

Is this really a graph problem?

Yes, it is about degrees in a directed graph. You never need to traverse the graph, though: counting degrees from the edge list is enough.