-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
56 lines (45 loc) · 1.58 KB
/
Solution.cs
File metadata and controls
56 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace LeetCode.Problem997{
//997. Find the Town Judge
//https://leetcode.com/problems/find-the-town-judge/
/*
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] = [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.
*/
public class Solution {
public int FindJudge(int n, int[][] trust) {
Dictionary<int,int> trusted = new Dictionary<int,int>();
var releation = new HashSet<int[]>(trust);
int citizen = n;
var judge = 0;
foreach (var item in trust)
{
if (!trusted.ContainsKey(item[0]))
trusted.Add(item[0],item[1]);
}
if (trusted.Count == n - 1)
{
while (citizen >= 1)
{
if (!trusted.ContainsKey(citizen))
judge = citizen;
citizen--;
}
for (int i = 1; i <= n; i++)
{
if (i == judge)
continue;
if (!releation.Where(p => p[0] == i).Select(p => p[1]).Contains(judge))
return -1;
}
return judge;
}
return -1;
}
}
}