Skip to content

Commit 2b4fb7b

Browse files
refactor 997
1 parent 4bd4971 commit 2b4fb7b

File tree

1 file changed

+15
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+15
-58
lines changed

src/main/java/com/fishercoder/solutions/_997.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,22 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6-
/**
7-
* 997. Find the Town Judge
8-
*
9-
* In a town, there are N people labelled from 1 to N.
10-
* There is a rumor that one of these people is secretly the town judge.
11-
*
12-
* If the town judge exists, then:
13-
*
14-
* The town judge trusts nobody.
15-
* Everybody (except for the town judge) trusts the town judge.
16-
* There is exactly one person that satisfies properties 1 and 2.
17-
* You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
18-
*
19-
* If the town judge exists and can be identified, return the label of the town judge.
20-
* Otherwise, return -1.
21-
*
22-
* Example 1:
23-
* Input: N = 2, trust = [[1,2]]
24-
* Output: 2
25-
*
26-
* Example 2:
27-
* Input: N = 3, trust = [[1,3],[2,3]]
28-
* Output: 3
29-
*
30-
* Example 3:
31-
* Input: N = 3, trust = [[1,3],[2,3],[3,1]]
32-
* Output: -1
33-
*
34-
* Example 4:
35-
* Input: N = 3, trust = [[1,2],[2,3]]
36-
* Output: -1
37-
*
38-
* Example 5:
39-
* Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
40-
* Output: 3
41-
*
42-
* Note:
43-
* 1 <= N <= 1000
44-
* trust.length <= 10000
45-
* trust[i] are all different
46-
* trust[i][0] != trust[i][1]
47-
* 1 <= trust[i][0], trust[i][1] <= N
48-
*/
496
public class _997 {
50-
public static class Solution1 {
51-
public int findJudge(int N, int[][] trust) {
52-
int[] trustPoints = new int[N];
53-
Set<Integer> trustOthers = new HashSet<>();
54-
for (int[] eachTrust : trust) {
55-
trustPoints[eachTrust[1] - 1]++;
56-
trustOthers.add(eachTrust[0]);
57-
}
58-
int judge = -1;
59-
for (int i = 0; i < trustPoints.length; i++) {
60-
if (trustPoints[i] == N - 1 && !trustOthers.contains(i + 1)) {
61-
judge = i + 1;
7+
public static class Solution1 {
8+
public int findJudge(int N, int[][] trust) {
9+
int[] trustPoints = new int[N];
10+
Set<Integer> trustOthers = new HashSet<>();
11+
for (int[] eachTrust : trust) {
12+
trustPoints[eachTrust[1] - 1]++;
13+
trustOthers.add(eachTrust[0]);
14+
}
15+
int judge = -1;
16+
for (int i = 0; i < trustPoints.length; i++) {
17+
if (trustPoints[i] == N - 1 && !trustOthers.contains(i + 1)) {
18+
judge = i + 1;
19+
}
20+
}
21+
return judge;
6222
}
63-
}
64-
return judge;
6523
}
66-
}
6724
}

0 commit comments

Comments
 (0)