Problem
Ask
Submissions

Problem: Find the Town Judge

Easy
15 min
Understand how to identify the town judge by applying graph theory and trust relationship analysis. This lesson helps you use coding patterns to solve problems involving directed graphs and trust networks efficiently. Practice developing algorithmic solutions that pinpoint a unique individual based on given constraints.

Statement

There are n people numbered from 11 to n in a town. There’s a rumor that one of these people is secretly the town judge. A town judge must meet the following conditions:

  1. The judge doesn’t trust anyone.

  2. Everyone else in the town (except the town judge) trusts the judge.

  3. There is exactly one person who fulfills both the above conditions.

You are given an integer n and a two-dimensional array, trust, where each entry trust[i]=[ai,bi]trust[i] = [a_i, b_i] indicates that the person labeled aia_i trusts the person labeled bib_i. If a trust relationship is not specified in the trust array, it does not exist. Your task is to determine the label of the town judge, if one can be identified. If no such person exists, return 1-1.

Constraints:

  • 11 \leqn 1000\leq 1000

  • 00 \leqtrust.length 104\leq 10^{4}

  • trust[i].length ==2 == 2

  • ai \ne bi

  • 11 \leqai , bi \leqn

  • All the pairs in trust are unique.

Problem
Ask
Submissions

Problem: Find the Town Judge

Easy
15 min
Understand how to identify the town judge by applying graph theory and trust relationship analysis. This lesson helps you use coding patterns to solve problems involving directed graphs and trust networks efficiently. Practice developing algorithmic solutions that pinpoint a unique individual based on given constraints.

Statement

There are n people numbered from 11 to n in a town. There’s a rumor that one of these people is secretly the town judge. A town judge must meet the following conditions:

  1. The judge doesn’t trust anyone.

  2. Everyone else in the town (except the town judge) trusts the judge.

  3. There is exactly one person who fulfills both the above conditions.

You are given an integer n and a two-dimensional array, trust, where each entry trust[i]=[ai,bi]trust[i] = [a_i, b_i] indicates that the person labeled aia_i trusts the person labeled bib_i. If a trust relationship is not specified in the trust array, it does not exist. Your task is to determine the label of the town judge, if one can be identified. If no such person exists, return 1-1.

Constraints:

  • 11 \leqn 1000\leq 1000

  • 00 \leqtrust.length 104\leq 10^{4}

  • trust[i].length ==2 == 2

  • ai \ne bi

  • 11 \leqai , bi \leqn

  • All the pairs in trust are unique.