997. Find the Town Judge

Find the Town Judge (LeetCode 997) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Frequency Map / Counting subpattern.

How to solve Find the Town Judge

Find the Town Judge is a Hashing Frequency Map and Counting problem, part of Array/Matrix Manipulation Patterns. Master hash map frequency counting for array problems. Count occurrences, find duplicates, and solve majority element problems efficiently.

Work towards the solution in these steps:

  1. Treat each trust pair as a directed edge from the trusting person to the trusted person.
  2. A judge must have no outgoing trust edges and must be trusted by every other person.
  3. Count, for each person, how many people they trust and how many people trust them.

The reference solution runs in O(n + trust.length) time and O(n) space.

The Find the Town Judge solution and editorial walks through the full approach with reference code.

Problem statement

There are n people, identified by the integers from 1 through n. Each inner array in trust has the form [a, b], meaning person a trusts person b.

A town judge is a person who trusts nobody, while every other person trusts the judge. Determine whether such a person exists. If it exists, return that person's label; otherwise, return -1.

Implement findJudge in the Solution class with the given signature. The input may describe no trust relationships, and the requirements must hold for all people exactly as stated.

Example 1:

Input: n = 4, trust = [[1,3],[2,3],[4,3]] Output: 3 Explanation: Person 3 trusts nobody, and each of the other three people trusts person 3, so person 3 is the judge.

Example 2:

Input: n = 3, trust = [[1,2],[2,3]] Output: -1 Explanation: Although person 3 is trusted by person 2, person 1 does not trust person 3, so no one satisfies both judge conditions.

Example 3:

Input: n = 1, trust = [] Output: 1 Explanation: With only one person, that person trusts nobody and there are no other people who need to trust the judge.

Constraints:

- 1 <= n <= 1000 - 0 <= trust.length <= 10^4 - trust[i].length == 2 - 1 <= trust[i][0] <= n - 1 <= trust[i][1] <= n - trust[i][0] != trust[i][1] - All pairs in trust are unique

Topics and companies

Topics: Graph, Array, Counting.

How to practise Find the Town Judge on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n + trust.length) time and O(n) space.

Open Find the Town Judge in the code editor, or read the Find the Town Judge editorial for a worked solution with its approach and complexity analysis.

Where Find the Town Judge sits in the DSA pattern sheet

Problems related to Find the Town Judge

Other problems that use the same Hashing - Frequency Map / Counting and Array/Matrix Manipulation Patterns techniques:

Find the Town Judge FAQ

What is the LeetCode number of Find the Town Judge?

Find the Town Judge is LeetCode problem 997, rated Easy.

What pattern is used to solve Find the Town Judge?

Find the Town Judge is a Hashing Frequency Map and Counting problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Hashing - Frequency Map / Counting subpattern.

What is the time complexity of Find the Town Judge?

The reference solution runs in O(n + trust.length) time and O(n) space.

Preparing this workspace.