-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
997.java
70 lines (62 loc) · 2.11 KB
/
997.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
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
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public int findJudge(int N, int[][] trust) {
int arr[] = new int[N+1];
int rv = -1;
//System.out.println(trust.length);
for(int i=0; i< trust.length; i++){
arr[trust[i][1]]++;
}
for(int j=0; j<arr.length; j++){
if(arr[j] == N-1){
rv = j;
}
}
//System.out.println(rv);
for(int i=0; i< trust.length; i++){
//System.out.println(trust[i][0]);
if(trust[i][0] == rv){
return -1;
}
}
return rv;
}
}
__________________________________________________________________________________________________
sample 55980 kb submission
class Solution {
public int findJudge(int N, int[][] trust) {
boolean isJudgeTrusted = true;
int judge = -1;
Map<Integer, List<Integer>> trustMap = new HashMap();
for (int i = 1; i <= N; i++) {
trustMap.put(i, new ArrayList<>());
}
for (int[] singleTrust : trust) {
int personOne = singleTrust[0];
int personTwo = singleTrust[1];
trustMap.computeIfPresent(personOne, (k,v) -> {
List<Integer> newTrusts = new ArrayList<>(v);
newTrusts.add(personTwo);
return newTrusts;
});
}
for (Map.Entry<Integer, List<Integer>> person : trustMap.entrySet()) {
if (person.getValue().isEmpty()) {
judge = person.getKey();
}
}
for (Map.Entry<Integer, List<Integer>> person : trustMap.entrySet()) {
if (person.getKey() != judge && !person.getValue().contains(judge)) {
isJudgeTrusted = false;
}
}
if (isJudgeTrusted) {
return judge;
} else {
return -1;
}
}
}
__________________________________________________________________________________________________