Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b9a4202

Browse files
add a solution for 997
1 parent 99a181b commit b9a4202

File tree

2 files changed

+71
-46
lines changed

2 files changed

+71
-46
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,42 @@
55

66
public class _997 {
77
public static class Solution1 {
8-
public int findJudge(int N, int[][] trust) {
9-
int[] trustPoints = new int[N];
8+
public int findJudge(int n, int[][] trust) {
9+
int[] trustPoints = new int[n];
1010
Set<Integer> trustOthers = new HashSet<>();
1111
for (int[] eachTrust : trust) {
1212
trustPoints[eachTrust[1] - 1]++;
1313
trustOthers.add(eachTrust[0]);
1414
}
1515
int judge = -1;
1616
for (int i = 0; i < trustPoints.length; i++) {
17-
if (trustPoints[i] == N - 1 && !trustOthers.contains(i + 1)) {
17+
if (trustPoints[i] == n - 1 && !trustOthers.contains(i + 1)) {
1818
judge = i + 1;
1919
}
2020
}
2121
return judge;
2222
}
2323
}
24+
25+
public static class Solution2 {
26+
/**
27+
* Credit: https://leetcode.com/problems/find-the-town-judge/solution/ solution 2
28+
*/
29+
public int findJudge(int n, int[][] trust) {
30+
if (trust.length < n - 1) {
31+
return -1;
32+
}
33+
int[] trustScores = new int[n];
34+
for (int[] t : trust) {
35+
trustScores[t[1] - 1]++;
36+
trustScores[t[0] - 1]--;
37+
}
38+
for (int i = 0; i < n; i++) {
39+
if (trustScores[i] == n - 1) {
40+
return i + 1;
41+
}
42+
}
43+
return -1;
44+
}
45+
}
2446
}

src/test/java/com/fishercoder/_997Test.java

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,50 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _997Test {
10-
private static _997.Solution1 solution1;
11-
private static int[][] trust;
12-
13-
@BeforeClass
14-
public static void setup() {
15-
solution1 = new _997.Solution1();
16-
}
17-
18-
@Test
19-
public void test1() {
20-
trust = new int[][] {{1, 2}};
21-
assertEquals(2, solution1.findJudge(2, trust));
22-
}
23-
24-
@Test
25-
public void test2() {
26-
trust = new int[][] {{1, 3}, {2, 3}};
27-
assertEquals(3, solution1.findJudge(3, trust));
28-
}
29-
30-
@Test
31-
public void test3() {
32-
trust = new int[][] {{1, 2}, {2, 3}, {3, 1}};
33-
assertEquals(-1, solution1.findJudge(3, trust));
34-
}
35-
36-
@Test
37-
public void test4() {
38-
trust = new int[][] {{1, 2}, {2, 3}};
39-
assertEquals(-1, solution1.findJudge(3, trust));
40-
}
41-
42-
@Test
43-
public void test5() {
44-
trust = new int[][] {{1, 3}, {1, 4}, {2, 3}, {2, 4}, {4, 3}};
45-
assertEquals(3, solution1.findJudge(4, trust));
46-
}
47-
48-
@Test
49-
public void test6() {
50-
trust = new int[][] {{1, 3}, {2, 3}, {3, 1}};
51-
assertEquals(-1, solution1.findJudge(3, trust));
52-
}
10+
private static _997.Solution1 solution1;
11+
private static _997.Solution2 solution2;
12+
private static int[][] trust;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _997.Solution1();
17+
solution2 = new _997.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
trust = new int[][]{{1, 2}};
23+
assertEquals(2, solution1.findJudge(2, trust));
24+
assertEquals(2, solution2.findJudge(2, trust));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
trust = new int[][]{{1, 3}, {2, 3}};
30+
assertEquals(3, solution1.findJudge(3, trust));
31+
}
32+
33+
@Test
34+
public void test3() {
35+
trust = new int[][]{{1, 2}, {2, 3}, {3, 1}};
36+
assertEquals(-1, solution1.findJudge(3, trust));
37+
}
38+
39+
@Test
40+
public void test4() {
41+
trust = new int[][]{{1, 2}, {2, 3}};
42+
assertEquals(-1, solution1.findJudge(3, trust));
43+
}
44+
45+
@Test
46+
public void test5() {
47+
trust = new int[][]{{1, 3}, {1, 4}, {2, 3}, {2, 4}, {4, 3}};
48+
assertEquals(3, solution1.findJudge(4, trust));
49+
}
50+
51+
@Test
52+
public void test6() {
53+
trust = new int[][]{{1, 3}, {2, 3}, {3, 1}};
54+
assertEquals(-1, solution1.findJudge(3, trust));
55+
}
5356
}

0 commit comments

Comments
 (0)