Skip to content

Commit c45c003

Browse files
authored
Update Find the Town Judge.java
1 parent 6a6bfb0 commit c45c003

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Easy/Find the Town Judge.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
class Solution {
2-
public int findJudge(int N, int[][] trust) {
3-
int[] count = new int[N + 1];
4-
5-
for (int[] item : trust) {
6-
count[item[0]]--;
7-
count[item[1]]++;
8-
}
9-
10-
for (int i = 1; i <= N; i++) {
11-
if (count[i] == N - 1) {
12-
return i;
13-
}
14-
}
15-
16-
return -1;
2+
public int findJudge(int n, int[][] trust) {
3+
int[] trustScore = new int[n + 1];
4+
for (int[] trustPair : trust) {
5+
trustScore[trustPair[1]]++;
6+
trustScore[trustPair[0]]--;
177
}
8+
for (int i = 1; i <= n; i++) {
9+
if (trustScore[i] == n - 1) {
10+
return i;
11+
}
12+
}
13+
return -1;
14+
}
1815
}

0 commit comments

Comments
 (0)