|
| 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() |
0 commit comments