Skip to content

Commit d3fdee0

Browse files
committed
LeetCode 997. Find the Town Judge
1 parent 30ff8f5 commit d3fdee0

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
317317
| [985. Sum of Even Numbers After Queries][lc985] | 🟠 Medium | [![python](res/py.png)][lc985py] |
318318
| [987. Vertical Order Traversal of a Binary Tree][lc987] | 🔴 Hard | [![python](res/py.png)][lc987py] |
319319
| [990. Satisfiability of Equality Equations][lc990] | 🟠 Medium | [![python](res/py.png)][lc990py] |
320+
| [997. Find the Town Judge][lc997] | 🟢 Easy | [![python](res/py.png)][lc997py] [![rust](res/rs.png)][lc997rs] |
320321
| [994. Rotting Oranges][lc994] | 🟠 Medium | [![python](res/py.png)][lc994py] |
321322
| [999. Available Captures for Rook][lc999] | 🟢 Easy | [![python](res/py.png)][lc999py] |
322323
| [1008. Construct Binary Search Tree from Preorder Traversal][lc1008] | 🟠 Medium | [![python](res/py.png)][lc1008py] |
@@ -1010,6 +1011,9 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
10101011
[lc990py]: leetcode/satisfiability-of-equality-equations.py
10111012
[lc994]: https://leetcode.com/problems/rotting-oranges/
10121013
[lc994py]: leetcode/rotting-oranges.py
1014+
[lc997]: https://leetcode.com/problems/find-the-town-judge/
1015+
[lc997py]: leetcode/find-the-town-judge.py
1016+
[lc997rs]: leetcode/find-the-town-judge.rs
10131017
[lc999]: https://leetcode.com/problems/available-captures-for-rook/
10141018
[lc999py]: leetcode/available-captures-for-rook.py
10151019
[lc1008]: https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/

leetcode/find-the-town-judge.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# 997. Find the Town Judge
2+
# 🟢 Easy
3+
#
4+
# https://leetcode.com/problems/find-the-town-judge/
5+
#
6+
# Tags: Array - Hash Table - Graph
7+
8+
import timeit
9+
from typing import List
10+
11+
12+
# We can solve this problem using topological sorting, create sets of
13+
# elements that are trusted by the current one and elements that the
14+
# current one trusts, return the element that does not trust any others
15+
# and is trusted by n-1 others.
16+
#
17+
# Time complexity: O(n+t) - Where n is n and t is the number of items
18+
# in the trust array. We iterate over all the elements in trust to
19+
# create the topological sorting, then iterate over a max of n elements
20+
# to find the one that matches the given conditions.
21+
#
22+
# Runtime 736 ms Beats 88.56%
23+
# Memory 18.9 MB Beats 59.7%
24+
class UseSets:
25+
def findJudge(self, n: int, trust: List[List[int]]) -> int:
26+
# In and outdegree arrays.
27+
trusted_by, trusts = [set() for _ in range(n + 1)], [
28+
set() for _ in range(n + 1)
29+
]
30+
for trustee, trusted in trust:
31+
trusted_by[trusted].add(trustee)
32+
trusts[trustee].add(trusted)
33+
for i in range(1, n + 1):
34+
if len(trusted_by[i]) == n - 1 and len(trusts[i]) == 0:
35+
return i
36+
# We can't determine the judge.
37+
return -1
38+
39+
40+
# We can solve this problem using topological sorting, count the
41+
# elements that are trusted by the current one and elements that the
42+
# current one trusts, return the element that does not trust any others
43+
# and is trusted by n-1 others.
44+
#
45+
# Time complexity: O(n+t) - Where n is n and t is the number of items
46+
# in the trust array. We iterate over all the elements in trust to
47+
# create the topological sorting, then iterate over a max of n elements
48+
# to find the one that matches the given conditions.
49+
#
50+
# Runtime 726 ms Beats 94.15%
51+
# Memory 18.9 MB Beats 59.7%
52+
class Count:
53+
def findJudge(self, n: int, trust: List[List[int]]) -> int:
54+
# In and outdegree arrays.
55+
trusted_by, trusts = [0] * (n + 1), [0] * (n + 1)
56+
for trustee, trusted in trust:
57+
trusted_by[trusted] += 1
58+
trusts[trustee] += 1
59+
for i in range(1, n + 1):
60+
if trusted_by[i] == n - 1 and trusts[i] == 0:
61+
return i
62+
# We can't determine the judge.
63+
return -1
64+
65+
66+
def test():
67+
executors = [
68+
UseSets,
69+
Count,
70+
]
71+
tests = [
72+
[1, [], 1],
73+
[2, [[1, 2]], 2],
74+
[3, [[1, 3], [2, 3]], 3],
75+
[3, [[1, 3], [2, 3], [3, 1]], -1],
76+
]
77+
for executor in executors:
78+
start = timeit.default_timer()
79+
for _ in range(1):
80+
for col, t in enumerate(tests):
81+
sol = executor()
82+
result = sol.findJudge(t[0], t[1])
83+
exp = t[2]
84+
assert result == exp, (
85+
f"\033[93m» {result} <> {exp}\033[91m for"
86+
+ f" test {col} using \033[1m{executor.__name__}"
87+
)
88+
stop = timeit.default_timer()
89+
used = str(round(stop - start, 5))
90+
cols = "{0:20}{1:10}{2:10}"
91+
res = cols.format(executor.__name__, used, "seconds")
92+
print(f"\033[92m» {res}\033[0m")
93+
94+
95+
test()

leetcode/find-the-town-judge.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 997. Find the Town Judge
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/find-the-town-judge/
5+
//
6+
// Tags: Array - Hash Table - Graph
7+
8+
struct Solution;
9+
impl Solution {
10+
// We can solve this problem using topological sorting, count the
11+
// elements that are trusted by the current one and elements that the
12+
// current one trusts, return the element that does not trust any others
13+
// and is trusted by n-1 others.
14+
//
15+
// Time complexity: O(n+t) - Where n is n and t is the number of items
16+
// in the trust array. We iterate over all the elements in trust to
17+
// create the topological sorting, then iterate over a max of n elements
18+
// to find the one that matches the given conditions.
19+
//
20+
// Runtime 14 ms Beats 100%
21+
// Memory 2.8 MB Beats 36%
22+
pub fn find_judge(n: i32, trust: Vec<Vec<i32>>) -> i32 {
23+
let mut trusted_by = vec![0; (n as usize) + 1];
24+
let mut trusts = vec![0; (n as usize) + 1];
25+
for t in trust {
26+
trusts[t[0] as usize] += 1;
27+
trusted_by[t[1] as usize] += 1;
28+
}
29+
for i in 1..=(n as usize) {
30+
if trusted_by[i] == (n as usize) - 1 && trusts[i] == 0 {
31+
return i as i32;
32+
}
33+
}
34+
-1
35+
}
36+
}
37+
38+
// Tests.
39+
fn main() {
40+
assert_eq!(Solution::find_judge(1, vec![]), 1);
41+
assert_eq!(Solution::find_judge(2, vec![vec![1, 2]]), 2);
42+
assert_eq!(Solution::find_judge(3, vec![vec![1, 3], vec![2, 3]]), 3);
43+
assert_eq!(
44+
Solution::find_judge(3, vec![vec![1, 3], vec![2, 3], vec![3, 1]]),
45+
-1
46+
);
47+
println!("All tests passed!")
48+
}

0 commit comments

Comments
 (0)