-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFindTheTownJudge.java
39 lines (31 loc) · 940 Bytes
/
FindTheTownJudge.java
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
package graph;
// Source : https://leetcode.com/problems/find-the-town-judge/
// Id : 997
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Topic : Graph
// Date : 2019-05-03
// Other :
// Tips :
// Result : 98.73% 100.00%
public class FindTheTownJudge {
//98.73% 100.00%
public int findJudge(int N, int[][] trust) {
boolean[] notJudge = new boolean[N + 1];
int[] beingTrusted = new int[N + 1];
for (int[] pair : trust) {
// if trust others, then not judge
notJudge[pair[0]] = true;
boolean not = notJudge[pair[0]];
int beTru = pair[1];
if (notJudge[pair[1]]) {
continue;
}
beingTrusted[pair[1]]++;
}
for (int i = 1; i < N + 1; i++) {
if (beingTrusted[i] == N - 1 && !notJudge[i])
return i;
}
return -1;
}
}