|
| 1 | +--- |
| 2 | +id: find-the-town-judge |
| 3 | +title: Find the Town Judge |
| 4 | +sidebar_label: Find the Town Judge |
| 5 | +tags: [Graph, Array, C++, Python, Java] |
| 6 | +description: Identify the town judge based on trust relationships in a town. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +If the town judge exists, then: |
| 16 | + |
| 17 | +1. The town judge trusts nobody. |
| 18 | +2. Everybody (except for the town judge) trusts the town judge. |
| 19 | +3. There is exactly one person that satisfies properties 1 and 2. |
| 20 | + |
| 21 | +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 such a trust relationship does not exist. |
| 22 | + |
| 23 | +Return the label of the town judge if the town judge exists and can be identified, or return `-1` otherwise. |
| 24 | + |
| 25 | +### Example |
| 26 | + |
| 27 | +**Example 1:** |
| 28 | +``` |
| 29 | +Input: n = 2, trust = [[1,2]] |
| 30 | +Output: 2 |
| 31 | +``` |
| 32 | +**Example 2:** |
| 33 | +``` |
| 34 | +Input: n = 3, trust = [[1,3],[2,3]] |
| 35 | +Output: 3 |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- $1 \leq n \leq 1000$ |
| 41 | +- $0 \leq \text{trust.length} \leq 10^4$ |
| 42 | +- `trust[i].length == 2` |
| 43 | +- All the pairs of trust are unique. |
| 44 | +- $a_i \ne b_i$ |
| 45 | +- $1 \leq a_i, b_i \leq n$ |
| 46 | + |
| 47 | +## Solution |
| 48 | + |
| 49 | +### Intuition |
| 50 | + |
| 51 | +To identify the town judge, we can use an array to keep track of the trust scores for each person. The trust score is increased by 1 for each person who trusts them and decreased by 1 for each person they trust. |
| 52 | + |
| 53 | +The town judge should have a trust score of `n-1` because they are trusted by everyone except themselves and they trust nobody. |
| 54 | + |
| 55 | +### Time Complexity and Space Complexity Analysis |
| 56 | + |
| 57 | +- **Time Complexity**: $O(n + \text{trust.length})$, where $n$ is the number of people and $\text{trust.length}$ is the number of trust relationships. |
| 58 | +- **Space Complexity**: $O(n)$, for the trust score array. |
| 59 | + |
| 60 | +### Code |
| 61 | + |
| 62 | +#### C++ |
| 63 | + |
| 64 | +```cpp |
| 65 | +class Solution { |
| 66 | +public: |
| 67 | + int findJudge(int n, vector<vector<int>>& trust) { |
| 68 | + vector<int> trustScores(n + 1, 0); |
| 69 | + |
| 70 | + for (const auto& t : trust) { |
| 71 | + trustScores[t[0]]--; |
| 72 | + trustScores[t[1]]++; |
| 73 | + } |
| 74 | + |
| 75 | + for (int i = 1; i <= n; ++i) { |
| 76 | + if (trustScores[i] == n - 1) { |
| 77 | + return i; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return -1; |
| 82 | + } |
| 83 | +}; |
| 84 | +``` |
| 85 | +
|
| 86 | +#### Python |
| 87 | +```python |
| 88 | +class Solution: |
| 89 | + def findJudge(self, n: int, trust: List[List[int]]) -> int: |
| 90 | + trust_scores = [0] * (n + 1) |
| 91 | + |
| 92 | + for a, b in trust: |
| 93 | + trust_scores[a] -= 1 |
| 94 | + trust_scores[b] += 1 |
| 95 | + |
| 96 | + for i in range(1, n + 1): |
| 97 | + if trust_scores[i] == n - 1: |
| 98 | + return i |
| 99 | + |
| 100 | + return -1 |
| 101 | +``` |
| 102 | + |
| 103 | +#### Java |
| 104 | +```java |
| 105 | +class Solution { |
| 106 | + public int findJudge(int n, int[][] trust) { |
| 107 | + int[] trustScores = new int[n + 1]; |
| 108 | + |
| 109 | + for (int[] t : trust) { |
| 110 | + trustScores[t[0]]--; |
| 111 | + trustScores[t[1]]++; |
| 112 | + } |
| 113 | + |
| 114 | + for (int i = 1; i <= n; i++) { |
| 115 | + if (trustScores[i] == n - 1) { |
| 116 | + return i; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return -1; |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
0 commit comments