8000 Add java solution2 for day10 · qilingit/Leetcode@4f8c26f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f8c26f

Browse files
committed
Add java solution2 for day10
1 parent fe56a1d commit 4f8c26f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

May-LeetCoding-Challenge/10-Find-The-Town-Judge/Find-The-Town-Judge.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,30 @@ public int findJudge(int N, int[][] trust) {
1818

1919
return -1;
2020
}
21+
}
22+
23+
class Solution2 {
24+
public int findJudge(int N, int[][] trust) {
25+
Set<Integer> possibleJudges = new HashSet<>();
26+
for (int i = 1; i <= N; i++) {
27+
possibleJudges.add(i);
28+
}
29+
for (int[] pair : trust) {
30+
possibleJudges.remove(pair[0]);
31+
}
32+
if (possibleJudges.size() == 1) {
33+
Set<Integer> trusters = new HashSet<>();
34+
int currentJudge = possibleJudges.iterator().next();
35+
for (int[] pair: trust) {
36+
if (pair[1] == currentJudge) {
37+
trusters.add(pair[0]);
38+
}
39+
}
40+
if (trusters.size() == N-1) {
41+
return currentJudge;
42+
}
43+
}
44+
return -1;
45+
46+
}
2147
}

0 commit comments

Comments
 (0)
0