Skip to content

Commit 11e5f1c

Browse files
add 997
1 parent 865e5b1 commit 11e5f1c

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|997|[Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_997.java) | O(n) | O(n) | |Easy|
3031
|994|[Rotting Oranges](https://leetcode.com/problems/rotting-oranges/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_994.java) | O(m*2*n*2) | O(m*n) | |Easy| BFS
3132
|993|[Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_993.java) | O(n) | O(m) (m is length of the nodes that has the max number of nodes on the same level) | |Easy| Tree, BFS
3233
|989|[Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_989.java) | O(max(N, logk)) | O(max(N, logk)) | |Easy| Array
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
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+
*/
49+
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;
62+
}
63+
}
64+
return judge;
65+
}
66+
}
67+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._997;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
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+
}
53+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy