-
Notifications
You must be signed in to change notification settings - Fork 0
/
997. Find the Town Judge
72 lines (67 loc) · 1.8 KB
/
997. Find the Town Judge
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution {
LinkedList<Integer> graph[];
int vertices;
public Solution(int n){
vertices = n;
graph = new LinkedList[n];
for(int i =0;i<n;i++)
graph[i] = null;
}
public Solution(){}
public void addEdge(int x, int y){
if(graph[x-1] == null){
graph[x-1] = new LinkedList<>();
}
graph[x-1].add(y);
}
public int checkIfEmpty(){
int count = 0;
int ans = -1;
for(int i =0;i<graph.length;i++)
{
if(graph[i] == null){
count++;
ans = i;
}
}
if(count == 1)
return ans;
return -1;
}
public boolean checkTrust(int indexOfJudge){
for(int i =0;i<graph.length;i++)
{
if(i == indexOfJudge)
continue;
Iterator it = graph[i].iterator();
boolean flag = false;
while(it.hasNext()){
Integer value = (Integer)it.next();
if(value == indexOfJudge+1){
flag = true;
break;
}
}
if(!flag) // not found
return flag;
}
return true;
}
public int findJudge(int n, int[][] trust) {
if(n== 0)
return -1;
if(n==1)
return 1;
if(trust.length == 0)
return -1;
Solution solution = new Solution(n);
for(int i =0;i<trust.length;i++){
solution.addEdge(trust[i][0], trust[i][1]);
}
int indexOfJudge = solution.checkIfEmpty();
if( indexOfJudge == -1) return -1;
if(solution.checkTrust(indexOfJudge))
return indexOfJudge+1;
return -1;
}
}