Skip to content

Commit d0df9fd

Browse files
authored
Merge pull request #1140 from Anshika14528/997
Added leetcode problem 997: #1111
2 parents e09a992 + 00b2303 commit d0df9fd

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
id: find-the-town-judge
3+
title: Find The Town Judge
4+
sidebar_label: 0997 Find The Town Judge
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Graph
9+
- LeetCode
10+
- C++
11+
description: "This is a solution to the Find The Town Judge problem on LeetCode."
12+
---
13+
14+
## Problem Description
15+
16+
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.
17+
18+
If the town judge exists, then:
19+
20+
1. The town judge trusts nobody.
21+
2. Everybody (except for the town judge) trusts the town judge.
22+
3. There is exactly one person that satisfies properties 1 and 2.
23+
24+
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.
25+
26+
Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
27+
28+
29+
30+
### Examples
31+
32+
**Example 1:**
33+
34+
```
35+
36+
Input: n = 2, trust = [[1,2]]
37+
Output: 2
38+
```
39+
40+
**Example 2:**
41+
42+
```
43+
Input: n = 3, trust = [[1,3],[2,3]]
44+
Output: 3
45+
```
46+
47+
**Example 2:**
48+
49+
```
50+
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
51+
Output: -1
52+
```
53+
54+
### Constraints
55+
56+
- $1 \leq \text{n} \leq 1000$.
57+
- $1 \leq \text{trust.length} \leq 10^4$.
58+
- $\text{trust[i].length} == 2$.
59+
- $\text{All the pairs of trust are unique}$.
60+
61+
62+
### Approach
63+
64+
To solve this problem(find the town judge) we simply have find that node which is not connected to anone but everyone is connected to it which means that node will be the town judge because according to the question the town judge trusts nobody but everybody (except for the town judge) trusts the town judge.
65+
66+
#### Code in C++
67+
68+
```cpp
69+
class Solution {
70+
public:
71+
int findJudge(int n, vector<vector<int>>& trust) {
72+
vector<vector<int>> adj(n + 1);
73+
vector<vector<int>> ad(n + 1);
74+
for (int i = 0; i < trust.size(); i++) {
75+
adj[t[i][0]].push_back(trust[i][1]);
76+
ad[t[i][1]].push_back(trust[i][0]);
77+
}
78+
for (int i = 1; i <= n; i++) {
79+
if (adj[i].size() == 0 && ad[i].size()==n-1) {
80+
return i;
81+
}
82+
}
83+
return -1;
84+
}
85+
};
86+
```
87+
88+
#### Code in Java
89+
90+
```java
91+
class Solution {
92+
public int findJudge(int n, int[][] trust) {
93+
int a[][] = new int[n][2];
94+
for(int i=0;i<trust.length;i++){
95+
int f = trust[i][0];
96+
int l = trust[i][1];
97+
a[l-1][0]++;
98+
a[f-1][1]++;
99+
}
100+
for(int i = 0; i < a.length; i++){
101+
int b[] = a[i];
102+
if(b[0] == n-1 && b[1] == 0)
103+
return i+1;
104+
}
105+
return -1;
106+
}
107+
}
108+
109+
```
110+
111+

0 commit comments

Comments
 (0)