Find the Town Judge - Complete Solution Guide
Find the Town Judge is LeetCode problem 997, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
In a town, there are n people labeled from 1 to n . There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2 . You are given an array trust where trust[i] = [a i , b i ] representing that the person labeled a i trusts the person labeled b i . If a trust relationship does not exist in trust array, then
Detailed Explanation
The problem asks us to find the 'town judge' in a town of 'n' people, labeled from 1 to 'n'. The town judge, if they exist, trusts nobody, and everyone else trusts the judge. We are given a list of trust relationships in the form of pairs (a, b), meaning person 'a' trusts person 'b'. Our task is to identify the judge based on these relationships or return -1 if no such judge exists. A judge must be trusted by n-1 people, and must trust no one.
Solution Approach
The solution uses two arrays, `trusts` (or `outDegree`) and `trusted` (or `inDegree`), to store the out-degree and in-degree of each person, respectively. We iterate through the `trust` array and increment `trusts[a]` and `trusted[b]` for each pair (a, b). After processing all trust relationships, we iterate from 1 to 'n' to find a person 'i' where `trusts[i]` is 0 (trusts nobody) and `trusted[i]` is 'n-1' (trusted by everyone else). If such a person is found, they are the town judge. If no such person is found, we return -1.
Step-by-Step Algorithm
- Step 1: Initialize two arrays, `trusts` (or `outDegree`) and `trusted` (or `inDegree`), of size n+1, with all elements set to 0.
- Step 2: Iterate through the `trust` array. For each pair (a, b), increment `trusts[a]` by 1 and increment `trusted[b]` by 1.
- Step 3: Iterate from 1 to n. For each person 'i', check if `trusts[i]` is 0 and `trusted[i]` is equal to n-1. If both conditions are true, return 'i' as the town judge.
- Step 4: If no judge is found after iterating through all people, return -1.
Key Insights
- Insight 1: The town judge trusts nobody. This means that if person 'i' is the town judge, 'i' will never appear as the first element (trustor) in any of the trust pairs.
- Insight 2: Everyone (except the town judge) trusts the town judge. This means that if person 'i' is the town judge, 'i' will be the second element (trustee) in n-1 trust pairs.
- Insight 3: We can use two arrays to track the number of times each person trusts someone else (out-degree) and the number of times each person is trusted by someone else (in-degree).
Complexity Analysis
Time Complexity: O(t + n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Graph.
Companies
Asked at: Arista Networks, Turing.