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:
You are given an array trust
where trust[i] = [ai, bi]
representing that the person labeled ai
trusts the person labeled bi
. If a trust relationship does not exist in trust
array, then such a trust relationship does not exist.
Return the label of the town judge if the town judge exists and can be identified, or return -1
otherwise.
Example 1:
Input: n = 2, trust = [[1,2]] Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]] Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]] Output: -1
Constraints:
1 <= n <= 1000
0 <= trust.length <= 104
trust[i].length == 2
trust
are unique.ai != bi
1 <= ai, bi <= n
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the town judge involves checking every person one by one to see if they fit the criteria to be the judge. We assume each person could potentially be the judge and then carefully check all the trust relationships against this assumption.
Here's how the algorithm would work step-by-step:
def find_town_judge_brute_force(number_of_people, trust_relationships):
for potential_judge in range(1, number_of_people + 1):
is_judge = True
# Check if everyone else trusts the potential judge
for other_person in range(1, number_of_people + 1):
if other_person != potential_judge:
trusts_potential_judge = False
for trust in trust_relationships:
if trust[0] == other_person and trust[1] == potential_judge:
trusts_potential_judge = True
break
if not trusts_potential_judge:
is_judge = False
break
if is_judge:
# Check if the potential judge trusts no one
trusts_someone_else = False
for trust in trust_relationships:
if trust[0] == potential_judge:
trusts_someone_else = True
break
# If no one is trusted, we found the judge
if not trusts_someone_else:
return potential_judge
# No one qualifies to be the judge
return -1
The key idea is to track how many people trust each person. We then look for someone who is trusted by everyone else but trusts nobody themselves. This approach avoids directly checking every possible candidate for the town judge.
Here's how the algorithm would work step-by-step:
def find_judge(number_of_people, trust_relationships):
trust_scores = [0] * (number_of_people + 1)
for trust_relation in trust_relationships:
trusting_person = trust_relation[0]
trusted_person = trust_relation[1]
# Reduce score for the truster, since a judge trusts nobody.
trust_scores[trusting_person] -= 1
trust_scores[trusted_person] += 1
# Find the person with a trust score of number_of_people - 1.
for person_index in range(1, number_of_people + 1):
if trust_scores[person_index] == number_of_people - 1:
# Verify if they actually trust anyone.
is_judge = True
for trust_relation in trust_relationships:
if trust_relation[0] == person_index:
is_judge = False
break
if is_judge:
return person_index
return -1
Case | How to Handle |
---|---|
Empty trust array and n = 1 | If only one person exists and no one trusts anyone, that person is the town judge. |
Empty trust array and n > 1 | If no trust relationships exist and more than one person exists, there can be no town judge. |
n = 2 and trust only contains one relationship where one person trusts the other | Check if the trusted person trusts anyone else; if they don't, they are the town judge. |
No one trusts anyone else | There is no town judge if everyone trusts only themselves, unless n = 1 |
Multiple potential town judges | The algorithm must verify that only one person meets the criteria for a town judge; if multiple are found, there is no valid judge. |
A person trusts themselves | The algorithm should ignore trust relationships where a person trusts themselves, as it's irrelevant to the problem. |
Cyclic trust relationships (A trusts B, B trusts A) | The algorithm counts trust relationships, so cycles will not inherently break the logic, but it's necessary to correctly identify the judge candidates. |
Integer overflow if n is close to max int and trust relationships are many | Use data types that can accommodate large numbers of trust relationships, considering `n` can be up to 1000. |